开发者

using fread to read into int buffer

开发者 https://www.devze.com 2023-04-09 22:03 出处:网络
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 pa开发者_Go百科rameter. So can\'t I just pass an integer

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 pa开发者_Go百科rameter. 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


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.


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.


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.


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).


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号