C 中文件存在的问题
这是我的代码,用于检查文件是否存在:
#include<stdio.h>
#include<zlib.h>
#include<unistd.h>
#include<string.h>
int main(int argc, char *argv[])
{
char *path=NULL;
FILE *file = NULL;
char *fileSeparator = "/";
size_t size=100;
int index ;
printf("\nArgument count is = %d", argc);
if (argc <= 1)
{
printf("\nUsage: ./output filename1 filename2 ...");
printf("\n The program will display human readable information about the PNG file provided");
}
else if (argc > 1)
{
for (index = 1; index < argc;index++)
{
path = getcwd(path, size);
strcat(path, fileSeparator);
printf("\n File name entered is = %s", argv[index]);
strcat(path,argv[index]);
printf("\n The complete path of the file name is = %s", path);
if (access(path, F_OK) != -1)
{
printf("File does exist");
}
else
{
printf("File does not exist");
}
path=NULL;
}
}
return 0;
}
运行命令 ./output test.txt test2.txt 输出为:
$ ./output test.txt test2.txt
Argument count is = 3
File name entered is = test.txt
The complete path of the file name is = /home/welcomeuser/test.txt
File does not exist
File name entered is = test2.txt
The complete path of the file name is = /home/welcomeuser/test2.txt
File does not exist
现在 test.txt 确实存在于系统上:
$ ls
assignment.c output.exe output.exe.stackdump test.txt
但 test.txt 显示为不存在的文件。
请帮助我理解这里的问题。另外,请随时发布任何改进代码/避免错误的建议。
问候, 黑暗者
Here is my code which checks if the file exists :
#include<stdio.h>
#include<zlib.h>
#include<unistd.h>
#include<string.h>
int main(int argc, char *argv[])
{
char *path=NULL;
FILE *file = NULL;
char *fileSeparator = "/";
size_t size=100;
int index ;
printf("\nArgument count is = %d", argc);
if (argc <= 1)
{
printf("\nUsage: ./output filename1 filename2 ...");
printf("\n The program will display human readable information about the PNG file provided");
}
else if (argc > 1)
{
for (index = 1; index < argc;index++)
{
path = getcwd(path, size);
strcat(path, fileSeparator);
printf("\n File name entered is = %s", argv[index]);
strcat(path,argv[index]);
printf("\n The complete path of the file name is = %s", path);
if (access(path, F_OK) != -1)
{
printf("File does exist");
}
else
{
printf("File does not exist");
}
path=NULL;
}
}
return 0;
}
On running the command ./output test.txt test2.txt
The output is:
$ ./output test.txt test2.txt
Argument count is = 3
File name entered is = test.txt
The complete path of the file name is = /home/welcomeuser/test.txt
File does not exist
File name entered is = test2.txt
The complete path of the file name is = /home/welcomeuser/test2.txt
File does not exist
Now test.txt does exist on the system:
$ ls
assignment.c output.exe output.exe.stackdump test.txt
and yet test.txt is shown as a file not existing.
Please help me understand the issue here. Also, please feel free to post any suggestions to improve the code/avoid a bug.
Regards,
darkie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅仅因为对
access()
的调用失败并不意味着该文件不存在。呼叫可能会因其他原因而失败。使用
printf("error:%s\n", strerror(errno));
打印出错误消息的文本。此外,您仍然错误地附加到从 getcwd 收到的“路径”,就像您在上一个问题中一样。即使它没有崩溃,它仍然不正确,并且可能会给您带来问题......甚至可能是您现在遇到的问题。
getcwd() 为您的路径分配一个缓冲区,但该缓冲区的大小仅适合路径。您正在附加到该缓冲区,超出末尾。这很糟糕,你不能这样做。它会导致问题,有时甚至会崩溃。您需要暂停并了解这个 getcwd 函数是如何工作的以及如何正确使用它。
Just because the call to
access()
fails does not mean that the file does not exist. The call could fail for other reasons.Use
printf("error:%s\n", strerror(errno));
to print out the text of the error message.Also you are still incorrectly appending to "path" received from getcwd as you were in your previous question. Even though it is not crashing, it is still not correct and could cause you problems... possibly even the problem you have now.
getcwd() allocates a buffer for your path, but that buffer is only sized to fit the path. you are appending to that buffer, going past the end. That's bad, you can't do that. It will cause problems, and occasionally crashes. you need to pause and understand how this getcwd function works and how to properly use it.
我强烈建议通过 malloc() 分配足够的空间来存储路径和fpathconf()(提示,路径最大)。
分配和组装它的非标准方法是 asprintf()。
只要确保在不再需要时释放生成的路径,并检查由于用户拼写错误而可能失败的每个调用是否失败。
如果使用 malloc(),请始终检查是否失败(结果为 NULL)。
祝你的任务顺利:)
I strongly suggest allocating enough room to store the path via malloc() and fpathconf() (hint, PATH_MAX).
A non-standard way of allocating and assembling it would be asprintf().
Just be sure to free the resulting path when its no longer needed, and check every call that could possibly fail due to user typos for failure.
If using malloc(), always check for failure (the result being NULL).
Good luck with your assignment :)