0

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

2
Contributors
3
Replies
5
Views
11 Years
Discussion Span
Last Post by Dave Sinkula
0

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?

0

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?

0
#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
*/
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.