9

I need to read and write binary data in C++.I use from ofstream and ifstream classes but it can't read some chars like 9,13,32.If is there another way to read and write theme.

4
  • 2
    Please show your code. Have you set std::ifstream::binary in the open-mode field? Commented Jun 13, 2011 at 11:35
  • Please show a snippet of code, the output you expect and the output you got. Commented Jun 13, 2011 at 11:35
  • please see stackoverflow.com/questions/5355163/… Commented Jun 13, 2011 at 11:50
  • The method I give in my answer works, but it performs relatively poorly. I would suggested reading the references the other answer gave because the functions mentioned actually perform a lot faster, though they are conceptually slightly more complex to use. Commented Jun 13, 2011 at 20:57

3 Answers 3

8

Open the file using the std::ios::binary flag and then use .read(buffer,length); and .write(buffer,length); rather than the streaming operators.

There are some examples here:

https://cplusplus.com/reference/istream/istream/read/

https://cplusplus.com/reference/ostream/ostream/write/

Sign up to request clarification or add additional context in comments.

2 Comments

@mehdi, he gave you documentation and examples. What more do you want?
Look at the example in the first link. It will open a file, read the contents and put the contents in to a char buffer. The buffer will be treated as an array of bytes essentially. Exactly what is it that you need more explanation of? How to handle the buffer and take values from it?
6

Here is a program that does this:

#include <iostream>
#include <fstream>

int main(int argc, const char *argv[])
{
   if (argc < 2) {
      ::std::cerr << "Usage: " << argv[0] << "<filename>\n";
      return 1;
   }
   ::std::ifstream in(argv[1], ::std::ios::binary);
   while (in) {
      char c;
      in.get(c);
      if (in) {
         ::std::cout << "Read a " << int(c) << "\n";
      }
   }
   return 0;
}

Here is an example of it being run in Linux:

$ echo -ne '\x9\xd\x20\x9\xd\x20\n' >binfile
$ ./readbin binfile 
Read a 9
Read a 13
Read a 32
Read a 9
Read a 13
Read a 32
Read a 10

4 Comments

Paranoid namespace resolution! :-)
@Kerrek SB: Why, yes, see stackoverflow.com/questions/1661912/… big grin
This works, but is very inefficient if the file is large. For better performance, use Stuard Golodetz method. For even better performance, use std::streambuf.
@user763305: Well, yes, it doesn't perform all that well. But the OP didn't seem in the mood for nuances. And the other answer just has links to documentation, so there is no method, just a hint as to how you might do it. :-)
3

This is a basic example (without any error check!):

// Required STL
#include <fstream>
using namespace std;

// Just a class example
class Data
{
   int    a;
   double b;
};

// Create some variables as examples
Data x;
Data *y = new Data[10];

// Open the file in input/output
fstream myFile( "data.bin", ios::in | ios::out | ios::binary );

// Write at the beginning of the binary file
myFile.seekp(0);
myFile.write( (char*)&x, sizeof (Data) );

...

// Assume that we want read 10 Data since the beginning
// of the binary file:
myFile.seekg( 0 );
myFile.read( (char*)y, sizeof (Data) * 10 );

// Remember to close the file
myFile.close( );

2 Comments

No standards-compliant compiler will build this file. std:: fstream is in <fstream> (no .h).
Applied suggestions.. sorry for the delay! Why didn't @johnsyweb edit my code?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.