我的程序是每隔一段时间获取文件大小并显示它以记录任何更改。由于某种原因,执行下面的代码挂起,只为我提供了一个光标。没有打印或显示任何内容。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
if (argc != 3)
{
printf("ERROR: Please enter correct amount of arguments.");
exit(0);
}
FILE *fp = fopen(argv[1], "r");
int time = atoi(argv[2]);
int size=0;
printf("Seconds File Size");
while(1)
{
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
sleep(4);
}
fclose(fp);
}
最佳答案
主要问题是,您将字符串输出到缓冲区,然后在确保缓冲区刷新到屏幕之前进入无限循环。
printf("Seconds File Size");
fflush(stdout);
其他问题:
- 您没有检查
fopen()
的结果 - 无法退出无限循环。
关于c - 程序在 printf 上挂起或跳过 printf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32752332/