使用 fread 读入 int 缓冲区

发布于 2024-12-07 17:49:05 字数 378 浏览 0 评论 0原文

我想知道是否可以使用 fread 将数据读入整数缓冲区。 我看到 fread() 将 void * 作为第一个参数。所以我不能只传递一个整数吗 buffer(类型转换为 void *),然后用它从文件中读取我想要的每个字节,只要缓冲区足够大?

IE。我不能这样做:

  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;

这里出了什么问题?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

帝王念 2024-12-14 17:49:05

如果您使用诸如 fwrite (“二进制”写入)之类的方式将整数写入文件,这应该可以工作。如果该文件是人类可读的(您可以使用文本编辑器打开它并查看有意义的数字),您可能需要 fscanf / cin

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.

献世佛 2024-12-14 17:49:05

正如其他人提到的那样, fread 应该能够做你想做的事
前提是输入是您期望的二进制格式。一个警告
我要补充的是,代码将具有平台依赖性并且
如果输入文件在之间移动,将无法正常工作
具有不同大小的整数或不同的平台
字节序 (sp)。

另外,你应该经常检查你的返回值;恐惧可能会失败。

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.

不及他 2024-12-14 17:49:05

是的,您可以使用 fread 读入整数数组

int buffer[10]; 

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

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

。您可以检查 fread 返回并打印出来的元素数量。

编辑:假设您正在以二进制模式读取文件,并且值被写入 fwrite 提到的 cnicutar 中。

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.

段念尘 2024-12-14 17:49:05

我正在尝试相同的方法,并得到与您相同的结果,当尝试使用 fread() 从文件中读取整数时,得到了大的 int 值,并最终得到了原因。
因此,假设您的输入文件仅包含:

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

我从 http://www.programmersheaven.com/mb/beginnercpp/396198/396198/fread-returns-invalid-integer/

fread() 读取二进制数据(即使文件以“文本”模式打开)。十六进制数 5403525650x203520350x20 是空格的 ASCII 码,0x35 是 ASCII '5' 的代码(由于使用的是小端机器,因此它们的顺序相反)。

因此,fread 的作用是从文件中读取 ASCII 代码,并从中构建一个 int,需要二进制数据。这应该可以解释读取 '5 5 5' 文件时的行为。当使用单个 '5' 读取文件时,也会发生同样的情况,但只能读取一个字节(如果后面跟有换行符,则只能读取两个字节),并且 fread 应该失败如果读取的内容少于 sizeof(int) 字节,即 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).

雨夜星沙 2024-12-14 17:49:05

由于响应的反应是仍然不起作用,我将在这里提供完整的代码,以便您尝试一下。

请注意,以下代码不包含正确的检查,如果文件不存在、没有剩余内存、没有权限等,可能会崩溃。
在代码中应该添加对每个打开、关闭、读、写操作的检查。

此外,我会动态分配缓冲区。

int* buffer = new int[10];

那是因为当普通数组作为指针时我感觉不太好。但无论如何。另请注意,根据数字范围,应使用正确的类型(uint32_t、16、8、int、short...)以节省空间。

以下代码将创建文件并写入正确的数据,然后您可以读取这些数据。

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文