gpt4 book ai didi

c - fopen 因我的文件路径而失败

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

尝试打开并写入以下文件路径,由于某种原因它无法打开,我尝试了几种不同的路径,但没有任何效果。我从 C:\运行这个

FILE* fptwo = fopen("\\C:\\Program Files\\NotMal.txt", "w");
if (fptwo != NULL)
{
printf("open progfiles successful \n");
//add this text to the file
fprintf(fptwo, "This is text");
printf("add text succuessful \n");

//close file pointer when finished
fclose(fptwo);
}

*如果在其他地方回答了这个问题,请随时删除或关闭这个问题,如果这是一个愚蠢的错误,我对其中所犯的错误表示歉意。

最佳答案

您测试一下是否正确打开了文件,没问题!但最好知道函数调用失败的原因。

对于“fopen”,可以通过查看errno的值来知道。 (阅读有关 fopen 的人)。还有一个“很酷的事情”是你可以使用“strerror”获得英文描述。

所以,就这样做:

#include <string.h> // For strerror
#include <errno.h> // For .... errno

FILE* fptwo = fopen("\\C:\\Program Files\\NotMal.txt", "w");
if (fptwo == NULL)
{
printf("Error : errno='%s'.\n", strerror(errno));
}
else
{
printf("open progfiles successful \n");
//add this text to the file
fprintf(fptwo, "This is text");
printf("add text succuessful \n");

//close file pointer when finished
fclose(fptwo);
}

关于c - fopen 因我的文件路径而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49278614/

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