gpt4 book ai didi

c - 如何知道 C 语言中是否存在文件?

转载 作者:行者123 更新时间:2023-11-30 20:55:58 25 4
gpt4 key购买 nike

我如何知道一个文件是否存在于我的 C 语言当前目录中?我正在提示用户,并尝试查看他们输入的文件是否已经存在。这是我得到的:

int a=0;
char ing[100];
FILE * inp;


while (a=0);
{
printf("Input file name: ");
scanf("%s", ing);
inp=fopen(ing , "r");
if (inp==NULL)
a=0;
else
a=1;
}

每当我输入不存在的文件时,我都会发现 cmd 没有响应。每当我输入存在的内容时,它都能正常工作,这很好。我只是不知道如果 NULL 不起作用,我到底需要输入什么。

最佳答案

替换:

while (a=0)

与:

while (a==0)

检查时使用:

if( access( ing, F_OK ) != -1 ) {
// exists
} else {
// not exist
}

关于c - 如何知道 C 语言中是否存在文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29311364/

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