- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有来自 cplusplus.com site 的简单代码
#include <stdio.h>
char mybuffer[80];
int main() {
FILE *pFile;
pFile = fopen("example.txt","r+");
if (pFile == NULL)
perror("Error opening file");
else {
fputs("test",pFile);
fflush(pFile); // flushing or repositioning required
fgets(mybuffer, 80, pFile);
puts(mybuffer);
fclose(pFile);
return 0;
}
}
我想知道 fflush
在 file
作为其参数的情况下到底做了什么,就像上面的代码一样,因为有和没有 结果是相同的fflush(pFile)
line - 空缓冲区(输出中没有任何内容)。
PS:我在 Linux gcc (6.3.0) 上运行代码
最佳答案
来自 C18 标准:
7.21.5.3 the
fopen
functionSynopsis
#nclude <stdio.h>
FILE *fopen(const char * restrict filename,
const char * restrict mode);Description
...
7 When a file is opened with update mode (
+
as the second or third character in the above list ofmode
argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to thefflush
function or to a file positioning function (fseek
,fsetpos
, orrewind
), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations.
以下是发布的代码执行的步骤
文件 example.txt 以读取和更新模式打开。
4 个字节(文本
)写入文件,覆盖文件的前 4 个字节。
流缓冲区通过 fflush()
刷新,允许模式从写入更改为读取。这就是注释所指的内容://需要刷新或重新定位
程序切换到读取模式而不更改位置,并尝试从文件中的位置 4 读取最多 79 个字节,并在换行符处停止。如果无法读取任何字节,则返回NULL
。
该行输出到标准输出。但请注意,如果文件包含 4 个字节或更少,fgets(mybuffer, 80, pFile)
会失败并返回 NULL
,留下数组 mybuffer
处于未确定的状态,导致 puts(mybuffer);
具有未定义的行为。
文件已关闭
关于c - Fflush 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56546651/
我认为 fsync() 在内部执行 fflush(),所以在流上使用 fsync() 是可以的。但是在网络 I/O 下执行时我得到了意想不到的结果。 我的代码片段: FILE* fp = fopen(
我有一个实现多线程网络代理的基本草图: FILE *proxy_log_file; static void SIGUSR1_handler(int sig) { (void)sig;
我遇到了 this page这说明了创建悬挂点的常见方式。 下面的代码通过返回局部变量的地址来说明悬空指针: // The pointer pointing to local variable bec
所以我写了一个测试程序,这是代码 #include #include int main(void) { int i; printf("%s", "entering\n");
有来自 cplusplus.com site 的简单代码 #include char mybuffer[80]; int main() { FILE *pFile; pFile =
我正在尝试使用以下代码将 2D 表格打印到我的终端: #include #include #include #include #include #include char getch()
我正在用 C 语言创建一个非常简单的程序执行计时器。我将在下面提供示例代码。问题是 fflush(NULL) 在以 Ctrl+C(即 SIGINT 信号)。它们有时仍会在 Estimated runn
我正在尝试用 C 语言制作一个时钟,但是屏幕没有正确清除,它只是不断地打印到一个新行。我如何不正确地使用 fflush? #include #include #include int main(
这个问题在这里已经有了答案: Using fflush(stdin) (7 个答案) How can one flush input stream in C? (8 个答案) 关闭 4 年前。 我似
我有这段代码:当第一个 child 向第二个 child 发送数据时,程序卡住了,第二个 child 做 fscanf 然后卡住了,因为他无法阅读我找不到它的原因。请帮助我找出如何确保 fflush
我很抱歉这个愚蠢的问题。我有 C 程序提示用户输入年龄和姓名然后将年龄和姓名打印到屏幕上。这是我从书上读到的练习。 这个程序: #include int main (void) { int a
我不确定我是否正确理解了 C 中刷新的工作原理。我就是无法按照多本手册和引用书中的描述让它工作。这是一个带有注释的示例: #include int main(void) { int x;
来自 std::fflush 文档(http://en.cppreference.com/w/cpp/io/c/fflush): Causes the output file stream to be
为什么 fflush(..) 对 c2 和 c0 不起作用? 如果我使用声明 c0 = 0 和 c2 = 0 它有效,但 fflush(stdin) 无效,我试着把在不同的地方但它没有用,我在 ubu
#include int main() { int test_no ,count=1,i,n,j; scanf("%d",&test_no); ffl
我开始编程并了解了以下命令fflush(stdout);。我明白为什么大多数平台都需要它,但我仍然问自己一些问题...... 1. \n 不是应该在 msys2 上缓冲吗,因为它是行缓冲的? (薄荷)
我似乎无法弄清楚这段代码有什么问题: #include #include #include #include #define MAX 100 #define TRUE 1 #define FA
#include int main() { int test_no ,count=1,i,n,j; scanf("%d",&test_no); ffl
我有以下代码, fflush(stdin); print("Enter y/n"); scanf("%c",&a); 这里,它在给出输入之前退出。看起来问题是因为它没有刷新可能包含一些垃圾字符的输入缓
因此,在 Google 上快速搜索用于清除输入缓冲区的 fflush(stdin) 会发现许多网站警告不要使用它。然而,这正是我的计算机科学教授教授全类的方式。 使用 fflush(stdin) 有多
我是一名优秀的程序员,十分优秀!