C程序中输入和输出之间的I/O [英] I/O between input and output in C programme

查看:96
本文介绍了C程序中输入和输出之间的I/O的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在APUE上看到了一个段落(第5.5章):

I saw a paragraph on APUE (chapter 5.5):

打开文件进行读写时,存在以下限制:
(1)如果没有介入 fflush ,fseek,fsetpos或倒带,则输入后不能直接跟随输出.
(2)如果没有介入fseek,fsetpos或 rewind 或遇到文件结尾的输入操作,则输入之后不能直接跟随输出.

When a file is opened for reading and writing, the following restrictions apply:
(1) Output cannot be directly followed by input without an intervening fflush, fseek, fsetpos,or rewind.
(2) Input cannot be directly followed by output without an intervening fseek, fsetpos,or rewind, or an input operation that encounters an end of file.

我可以想象为什么我们应该使用fseekfsetpos.但是为什么我应该在情况(1)中使用 fflush 而在情况(2)中使用 rewind .
如果在情况(1)中使用fflush,则没有任何内容可读取.而且,如果在情况(2)中使用倒带,则要输稿的内容将与原始内容重叠.我对吗?

I can imagine why we should use fseek, fsetpos. But why I should use fflush in case (1) and rewind in case (2).
If we use fflush in case (1), there is nothing left to read. And if we use rewind in case (2), something to be ouputed will overlap with original content. Am I right?

我尝试了2个实验:

int main() {
    FILE *file_p = fopen("new.txt", "r+");
    char buf[1024];
    char *str;

    fputs("hongkong\n", file_p);
    fputs("shanghai\n", file_p);

    fflush(file_p);

    fputs(fgets(buf, 1024, file_p), stderr);

    fclose(file_p);
    return 0;
}
/* Segmentation fault */


int main() {
    FILE *file_p = fopen("new.txt", "r+");
    char buf[1024];
    char *str;

    fgets(buf, 1024, file_p);
    rewind(file_p);
    fputs("hongkong\n", file_p);

    fclose(file_p);
    return 0;
}
/* origin text in new.txt is:
   shanghai
   taipei
   after execution:
   hongkong
   taipei
*/

在此段落中我是否有误解?

Is there something I misunderstood with this paragraph?

推荐答案

1)将数据写入文件时,它不会直接进入文件,而是进入缓冲区.最终填充完毕后,将文件写入.所有这些都是为了提高性能. fflush()的用途之一是将缓冲区中的数据保存到文件中,而不管该缓冲区是否为一半.这就像数据库的提交.因此,如果要读取文件,则需要确保所有数据都在其中.

1) When you write data into a file, it does not go to the file directly, it goes to a buffer. When it gets filled finally it leaves the file written. All this is to improve performance. One of fflush() uses is for save the data from the buffer into the file regardless if this buffer is half filled. It is like a commit for DBs. For that reason, if you want to read a file, before you need to be sure that all data is there.

2)提醒要获取文件,存在一个随文件一起移动的游标.如果将数据寄存器写入文件,则光标将位于文件的最后一个位置(或之前的位置+1),因此,如果在此之后读取它,则将到达EOF(以防万一)您在最后一个寄存器中.)

2) Remind that to fetch files exists a cursor that goes along the file. If you write a register of data into the file, the cursor will be in the last position of the file (or in the position + 1 where you were before), so if you read it after that, you will reach EOF (in case you were in the last register).

在您的第一个示例中:

 fputs(fgets(buf, 1024, file_p), stderr);

fgets()到达EOF,因此fputs()会给您带来分段错误,因为它接收到NULL.

fgets() reaches EOF, for that reason fputs() bring you a segmentation fault because it is receiving NULL.

在第二个中,您覆盖第一行.

In the second one you are overwriting the first line.

这篇关于C程序中输入和输出之间的I/O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆