简体   繁体   English

以奇怪的方式使用 fwrite

[英]Using fwrite in strange way

If I write如果我写

if((fp = fopen(some_path, "wb")))
{   
    int a = 50000;
    fwrite("c",sizeof(char),a,fp);
    fclose(fp);
    fp = fopen(some_path, "rb");
    char arr[50000];
    fread(arr, sizeof(char), a, fp);
    printf("%s\n", arr);
    fclose(fp);
}

it prints "c" of course but the file is ~50kb它当然会打印“c”,但文件约为 50kb

My questions are:我的问题是:

  1. How is this actually working?这实际上是如何工作的?
  2. If I modify var a to 60000 the executable crashes, so i`m thinking about some internal buffer of fwrite.如果我将 var a 修改为 60000,可执行文件会崩溃,所以我正在考虑 fwrite 的一些内部缓冲区。 How do i get its max capacity?我如何获得其最大容量?
  3. What does fwrite() write to the file in order to get the file to ~50kb of size and still print only "c"(I was expecting some mambo-jumbo characters here)? fwrite() 将什么写入文件以使文件大小达到约 50kb 并且仍然只打印“c”(我在这里期待一些 mambo-jumbo 字符)?
  4. How wrong is this usage of the function, I want to write a blank file of a certain size really fast(with dummy data), would I be wrong exploiting this behaviour in order not to make a big buffer and use up memory to write "real" data but still reduce fwrite calls(I may need to write a 10 gb file for ex.)? function 的这种用法有多么错误,我想非常快地写一个一定大小的空白文件(带有虚拟数据),我会错误地利用这种行为来不做一个大缓冲区并用完 memory 来写“真实”数据,但仍然减少 fwrite 调用(我可能需要写一个 10 GB 的文件,例如)?

How is this actually working?这实际上是如何工作的?

I would argue that it isn't working.我会争辩说它不起作用。 You did something nonsense, and it went uncaught.你做了一些废话,它没有被抓住。 The gave you the impression that it will work in the future.给你的印象是它会在未来发挥作用。 That's a failure.那是失败的。

If I modify var a to 60000 the executable crashes, so i`m thinking about some internal buffer of fwrite.如果我将 var a 修改为 60000,可执行文件会崩溃,所以我正在考虑 fwrite 的一些内部缓冲区。 How do i get its max capacity?我如何获得其最大容量?

There's no buffer.没有缓冲区。 You are merely accessing whatever is in memory after the c␀ created by "c" .您只是在"c"创建的 c␀ 之后访问c␀中的任何内容。 When it crashes, it's because you've reached a memory page that can't be read (eg hasn't been allocated).当它崩溃时,这是因为您到达了一个无法读取的 memory 页面(例如,尚未分配)。

What does fwrite() write to the file in order to get the file to ~50kb of size fwrite() 将什么写入文件以使文件大小达到约 50kb

Whatever happens to be in memory at the address returned by "c" and beyond.无论发生在 memory 中由"c"返回的地址处及以后。

and still print only "c"(I was expecting some mambo-jumbo characters here)?并且仍然只打印“c”(我在这里期待一些 mambo-jumbo 字符)?

It doesn't print only c .它不仅仅打印c Try something like hexdump -C file or od -c file尝试类似hexdump -C fileod -c file

How wrong is this usage of the function function 的这种用法有多么错误

Incredibly.难以置信。 It could crash for any value of a larger than 2.对于任何大于 2 a值,它都可能崩溃。

I want to write a blank file of a certain size really fast(with dummy data)我想非常快地写一个一定大小的空白文件(带有虚拟数据)

The docs for truncate says: " If the file previously was shorter, it is extended, and the extended part reads as null bytes ('\0'). " So you could use the following: truncate的文档说:“如果文件以前更短,它会被扩展,并且扩展部分读取为 null 字节('\0')。 ”所以你可以使用以下内容:

if (truncate(path, length)) {
   perror("truncate");
   exit(1);
}

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

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