尝试打开并写入以下文件路径(由于某种原因未打开),我尝试了几种不同的路径,但没有任何效果。我正在从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的值来知道。 (阅读有关开曼的男人的话​​)。
一个“很酷的事情”是,您可以使用“ 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/

10-12 00:07
查看更多