gpt4 book ai didi

c - 可能出了什么问题? fopen 错误

转载 作者:行者123 更新时间:2023-11-30 16:40:18 25 4
gpt4 key购买 nike

我的程序开头有以下代码片段:

printf("Starting extraction of file %s \n", tarName);

// Open the tarFile
FILE* tarFile = fopen(tarName, "r");
if(tarFile == NULL) return EXIT_FAILURE;

// Read nFiles
printf("Reading header...");
...

当我从终端执行它时,我得到以下输出:

Starting extraction of file test.mytar 

然后程序卡住,显然永远不会到达第二个 printf .

test.mytar是与我的可执行文件位于同一文件夹中的现有文件,这与我执行终端的文件夹位于同一文件夹中。

该文件是由我逐字节创建的,因此它可能违反了我不知道的文件约定。

这里可能发生了什么?

最佳答案

正如评论中指出的,可能会发生两种情况。

a) fopen 失败(IO 错误、权限被拒绝、文件丢失……)。要了解确切的原因,您需要打印 errno(或 Windows 上的 GetLastError()):

if(tarFile == NULL) {
printf("%s\n", strerror(errno));
return EXIT_FAILURE;
}

b) fopen 成功,但 printf("Reading header..."); 没有显示任何内容,因为消息已缓冲但尚未打印。要纠正此问题,您通常可以在消息末尾添加 '\n'

关于c - 可能出了什么问题? fopen 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46727249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com