hi
i need to r write binary(hexadecimal) values into a text file then read this data and manipulate it as a number
:sad:

Recommended Answers

All 3 Replies

i need to r write binary(hexadecimal) values into a text file then read this data and manipulate it as a number

So is it binary or is it text? Do you mean that you want to write the binary text representation of a value and read this text back as its value?

i write a hexadecimal value on a file using "ofstream" then i want to read this value from a file (may be as a string) and then convert it to its hexadecimal value then manipulate it(i.e add,subtract...........)
so can i do that?

#include <iostream>
#include <fstream>

void write(const char *filename)
{
   std::ofstream file(filename);
   for ( int i = 100; i < 110; ++i )
   {
      file << std::hex << i << '\n';
   }
}

void read(const char *filename)
{
   std::ifstream file(filename);
   int value;
   while ( file >> std::hex >> value )
   {
      std::cout << "value = " << value << '\n';
   }
}

int main()
{
   static const char filename[] = "file.txt";
   write(filename);
   read(filename);
   return 0;
}

/* my output
value = 100
value = 101
value = 102
value = 103
value = 104
value = 105
value = 106
value = 107
value = 108
value = 109
*/

/* file.txt
64
65
66
67
68
69
6a
6b
6c
6d
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.