简体   繁体   English

fseek和fread C编程

[英]fseek and fread C programming

EDIT: Thanks everyone for the quick answers. 编辑:谢谢大家的快速解答。 :) :)

Ok i am new in C and i am able to open a file and set to a certain position in the file and read a chunk of data and write it to another file using this code below: 好的,我是C语言的新手,我可以打开一个文件并将其设置为文件中的特定位置,并使用以下代码读取数据块并将其写入另一个文件:

#include <stdio.h>

int main (int argc, unsigned char *argv[])

  {

      FILE* in = fopen(argv[1], "rb");
      FILE* out = fopen("test.bin", "wb");

      unsigned char buffer[0x200];

      fseek(in, 0x8F00, SEEK_SET);
      fread(buffer, sizeof(buffer), 1, in);   
      fwrite(buffer, sizeof(buffer), 1, out);
  };


But i am kind of versed in perl and i can easily seek to any position in the file and read a chunk of data like this: 但是我很熟悉perl ,我可以轻松地查找文件中的任何位置并读取像这样的数据块:

seek ( $file, 0x8F00, 0);
read ( $file, $buffer, 0x200);

As you can see i dont have to pre-declare my buffer size in perl , i can specify buffer in the read function itself. 如您所见,我不必在perl中预先声明我的缓冲区大小,我可以在read函数本身中指定缓冲区。 Is there anyway at all i can use buffer in C without having to predeclare it like in perl ? 无论如何,我完全可以在C中使用buffer而不用像在perl中那样预先声明它吗?

Is there anyway at all I can use buffer in C without having to predeclare it like in perl? 无论如何,我完全可以在C中使用buffer而不用像在perl中那样预先声明它吗?

No. 没有。

C is a lower-level language than Perl and does not have magic self-resizing strings, buffers, or arrays. C是比Perl低的语言,并且没有魔术般的自动调整大小的字符串,缓冲区或数组。 Your options are to allocate a huge buffer that's bigger than you anticipate ever needing, or read your file incrementally into a buffer that you resize as needed. 您的选择是分配一个比预期的要大的巨大缓冲区,或者将文件逐步读取到您需要调整大小的缓冲区中。

No, in C you need to provide a buffer size upfront, and pass that buffer to the fread function. 不,在C语言中,您需要预先提供缓冲区大小,然后将该缓冲区传递给fread函数。

You do not need to specify a constant buffer size, though: the size of 0x200 in your case could be calculated at run-time, for example, by examining the size of the file that you are reading. 但是,您无需指定恒定的缓冲区大小:您情况下的0x200大小可以在运行时计算出来,例如,通过检查正在读取的文件的大小来计算。

Note, however, that when your program selects the size of the buffer dynamically, you should place the buffer in the dynamic memory area, rather than leaving it in the automatic area (ie on the stack). 但是请注意,当程序动态选择缓冲区的大小时,应将缓冲区放置在动态内存区域中,而不是将其保留在自动区域中(即,在堆栈上)。 This lets you avoid stack overflows when the file is too large, because the restrictions on the automatic memory are usually a lot more severe than the restrictions on the dynamic memory. 当文件太大时,这可以避免堆栈溢出,因为对自动内存的限制通常比对动态内存的限制要严格得多。

You could also write code that populates the buffer incrementally, expanding it as needed using realloc . 您还可以编写代码来逐步填充缓冲区,并根据需要使用realloc对其进行扩展。 See this answer for an example of expanding the read buffer dynamically. 请参阅此答案以获取动态扩展读取缓冲区的示例。

no, at least not using standard library. 不,至少不使用标准库。

This is because by default C does not handle automatically dinamic memory, as this kind of control is left to the user. 这是因为默认情况下C不会自动处理动态内存,因为这种控件留给用户使用。

you can easly go to the end of the file, read how many byte there are, then jump to your location, create a buffer of size file_size - file_location and do your rest-of-file reading 您可以轻松地转到文件的末尾,读取有多少字节,然后跳转到您的位置,创建一个大小为file_size - file_location的缓冲区,并进行剩余文件读取

fseek(in, 0L, SEEK_END);
file_size = ftell(fp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM