using fread to read into int buffer

12,922

Solution 1

This should work if you wrote the ints to the file using something like fwrite ("binary" write). If the file is human-readable (you can open it with a text editor and see numbers that make sense) you probably want fscanf / cin.

Solution 2

As others have mentioned fread should be able to do what you want provided the input is in the binary format you expect. One caveat I would add is that the code will have platform dependencies and will not function correctly if the input file is moved between platforms with differently sized integers or different endian-nesses (sp).

Also, you should always check your return values; fread could fail.

Solution 3

Yes you can use fread to read into an array of integers

int buffer[10]; 

size_t readElements = fread((void *)buffer, sizeof(int), 10, somefile); 

for(int i = 0; i < readElements; i++) 
   cout << buffer[i] << endl

You can check the number of elements fread returns to print out.

EDIT: provided you are reading from a file in binary mode and the values were written as cnicutar mentioned with fwrite.

Solution 4

I was trying the same and was getting the same result as yours, large int value when trying to read integer using fread() from a file and finally got the reason for it. So suppose if your input file contains only:

  1. "5"
  2. "5 5 5"

The details I got from http://www.programmersheaven.com/mb/beginnercpp/396198/396198/fread-returns-invalid-integer/

fread() reads binary data (even if the file is opened in 'text'-mode). The number 540352565 in hex is 0x20352035, the 0x20 is the ASCII code of a space and 0x35 is the ASCII code of a '5' (they are in reversed order because using a little-endian machine).

So what fread does is read the ASCII codes from the file and builds an int from it, expecting binary data. This should explain the behavior when reading the '5 5 5' file. The same happens when reading the file with a single '5', but only one byte can be read (or two if it is followed by a newline) and fread should fail if it reads less than sizeof(int) bytes, which is 4 (in this case).

Solution 5

As the reaction to response is that it still does not work, I will provide here complete code, so you can try it out.

Please note that following code does NOT contain proper checks, and CAN crash if file does not exist, there is no memory left, no rights, etc. In code should be added check for each open, close, read, write operations.

Moreover, I would allocate the buffer dynamically.

int* buffer = new int[10];

That is because I do not feel good when normal array is taken as pointer. But whatever. Please also note, that using correct type (uint32_t, 16, 8, int, short...) should be done to save space, according to number range.

Following code will create file and write there correct data that you can then read.

    FILE* somefile;
    somefile = fopen("/root/Desktop/CAH/scripts/cryptor C++/OUT/TOCRYPT/wee", "wb");

  int buffer[10];
  for(int i = 0; i < 10; i++)
    buffer[i] = 15;

  fwrite((void *)buffer, sizeof(int), 10, somefile);
  // print contents of buffer
  for(int i = 0; i < 10; i++)
  cout << buffer[i] << endl;

  fclose(somefile);

    somefile = fopen("/root/Desktop/CAH/scripts/cryptor C++/OUT/TOCRYPT/wee", "rb");

  fread((void *)buffer, sizeof(int), 10, somefile);
  // print contents of buffer
  for(int i = 0; i < 10; i++)
  cout << buffer[i] << endl;

  fclose(somefile);
Share:
12,922
bespectacled
Author by

bespectacled

Researcher

Updated on June 05, 2022

Comments

  • bespectacled
    bespectacled almost 2 years

    I would like to know if I can use fread to read data into an integer buffer. I see fread() takes void * as the first parameter. So can't I just pass an integer buffer (typecast to void *) and then use this to read howmuchevery bytes I want to from the file, as long as the buffer is big enough ?

    ie. cant i do:

      int buffer[10];
      fread((void *)buffer, sizeof(int), 10, somefile);
      // print contents of buffer
      for(int i = 0; i < 10; i++)
      cout << buffer[i] << endl;
    

    What is wrong here ?

    Thanks

  • bespectacled
    bespectacled over 12 years
    No. It still prints weird numbers. That are not there in the file !
  • Jonathan Leffler
    Jonathan Leffler about 12 years
    More or less...fread() reads the bytes (which need not be ASCII codes since ASCII was a 7-bit system and bytes are normally (essentially always) 8-bit quantities). And then, when those bytes are interpreted as an integer, you get the result you describe (on a little-endian machine). If the binary data was not written with fwrite() on a machine sufficiently similar to the one you read the data on, there's no reason to suppose fread() will make sense of the data.