Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C stdlib fopen在将文件名作为变量传递时找不到文件_C_Windows_Visual Studio - Fatal编程技术网

C stdlib fopen在将文件名作为变量传递时找不到文件

C stdlib fopen在将文件名作为变量传递时找不到文件,c,windows,visual-studio,C,Windows,Visual Studio,我正在读取用户输入并获得正确的字符串,但当我尝试将相同的字符串传递给fopen时,它找不到文件 当我将文件名硬编码为字符串到函数调用中时,它可以工作,但是使用任何文件编写函数都会出现某种错误 老实说,感觉什么都不管用。我尝试了完整的文件路径,将文件移动到运行目录并重新打开VisualStudio。这两个问题都存在 我能确定我的stdlib没有损坏吗 编辑: 由于您的帮助,文件名现在已正确解析 但我仍然有以下问题: struct time { char* desc; int mon

我正在读取用户输入并获得正确的字符串,但当我尝试将相同的字符串传递给fopen时,它找不到文件

当我将文件名硬编码为字符串到函数调用中时,它可以工作,但是使用任何文件编写函数都会出现某种错误

老实说,感觉什么都不管用。我尝试了完整的文件路径,将文件移动到运行目录并重新打开VisualStudio。这两个问题都存在

我能确定我的stdlib没有损坏吗

编辑: 由于您的帮助,文件名现在已正确解析

但我仍然有以下问题:

struct time {
    char* desc;
    int month;
    int day;
    int hour;
};

void saveToFile(unsigned int count, struct time** reservations) {
    char* file = strtok(NULL, " ");
    char* clean = calloc(strlen(file), sizeof(char));
    if (clean != NULL) {
        strncpy(clean, file, strlen(file) - 1);
        FILE* writeTo = fopen(clean, "w");
        if (writeTo != NULL) {
            for (unsigned int i = 0; i < count; i++) {
                char s[80] = { 0 };
                sprintf(s, "A %s %d %d %d\n", reservations[i]->desc, reservations[i]->month,
                    reservations[i]->day, reservations[i]->hour);
                printf("%s", s);
                fprintf(writeTo, "A %s %d %d %d", reservations[i]->desc, reservations[i]->month,
                    reservations[i]->day, reservations[i]->hour);
            }
            if (ferror) printf("An error occurred while saving the data...\n");
            else printf("The calendar has successfully been saved to \"%s\"\n", clean);
            fclose(writeTo);
        }
        else printf("Could not find file...");
    }
}
结构时间{ char*desc; 整月; 国际日; 整小时; }; void saveToFile(无符号整数计数,结构时间**保留){ char*file=strtok(空,“”); char*clean=calloc(strlen(文件),sizeof(char)); 如果(干净!=NULL){ strncpy(干净、文件、strlen(文件)-1); 文件*writeTo=fopen(干净,“w”); if(writeTo!=NULL){ for(无符号整数i=0;idesc,预订[i]>month, 预订[i]->天,预订[i]->小时); printf(“%s”,s); fprintf(写入“A%s%d%d%d”,预订[i]>desc,预订[i]>month, 预订[i]->天,预订[i]->小时); } 如果(ferror)printf(“保存数据时出错…\n”); else printf(“日历已成功保存到\%s\”\n,干净); fclose(书面); } else printf(“找不到文件…”); } }
现在运行这段代码似乎正确地写入文件,但设置了ferror标志,我得到了相应的打印。ERRNO是0。我如何才能找到原因?

您的stdlib不太可能已损坏,您必须向我们展示您的代码,以便我们找到错误


可能您输入的最后有一个\n。

您最好粘贴失败的代码。现在不能。好的,这个问题可能在您这样做之前不会得到答案-同时人们可能会投反对票,因此您可能希望暂时删除它,并在输入代码后取消删除。还包括您使用的输入,以及您得到的错误的准确描述。不过,我现在就告诉您,它不会是一个损坏的stdlib。如何读取“正确的字符串”?它是与fgets()一起使用的吗?您是否记得删除行尾(CRLF,尽管您应该只看到换行符,也称为LF)?你如何证明你得到了正确的字符串?我推荐类似于
printf(“文件名:[[[%s]]]\n”,文件名)(如果愿意,可以使用不同的字符代替括号)。文件名前后的已知字符很重要;它们非常有助于发现文件名中仍存在CR之类的问题。