【问题标题】:fopen not opening Filefopen 打不开文件
【发布时间】:2015-02-19 01:45:09
【问题描述】:

我很确定这是打开文件的方式:

int readFile(char *filename){
char *source = NULL;
int fileSize;
char nameFile[strlen(filename)];
strcpy(nameFile,filename);
puts(nameFile);//Check if the string is correct
FILE *fp;

if ((fp =fopen(nameFile, "r")) != NULL) {
    /* Go to the end of the file. */
    if (fseek(fp, 0L, SEEK_END) == 0) {
        /* Get the size of the file. */
        long bufsize = ftell(fp);
        if (bufsize == -1) { /* Error */ }

        /* Allocate our buffer to that size. */
        source = malloc(sizeof(char) * (bufsize + 1));
        fileSize = (sizeof(char) * (bufsize + 1));

        /* Go back to the start of the file. */
        if (fseek(fp, 0L, SEEK_SET) != 0) { /* Error */ }

        /* Read the entire file into memory. */
        size_t newLen = fread(source, sizeof(char), bufsize, fp);
        if (newLen == 0) {
            fputs("Error reading file", stderr);
        } else {
            source[++newLen] = '\0'; /* Just to be safe. */
        }
    }
    fclose(fp);
} else{
    printf("File Doesnt Exist. :( \n");
    return 0;
}

所以,我在我的工作目录中。当我编译时,可执行文件是在文件所在的位置创建的。我做了一个 puts 来比较文件的名称。文件名(与终端显示的相同); 我将“r”切换为“r+”,但我仍然发现我的文件不存在。 另外,我做了 fopen("actualFileName.txt") 并且它确实有效......所以......有什么想法吗??

这就是我调用 readFile 的方式:

fgets(userInput,sizeof(userInput),stdin);
readFile(userInput);

【问题讨论】:

  • 你试过char nameFile[strlen(filename)+1];nameFile[strlen(filename)]='\0'吗?闻起来像离我更近的失踪者!
  • 为什么要复制文件名
  • @pm100 你说得对,首先不需要复制文件名。他可能正在尝试调试 fopen 失败的原因
  • 以下建议使用 perror 会有所帮助

标签: c string stdin fopen


【解决方案1】:

这是错误的

char nameFile[strlen(filename)];

应该是

char nameFile[1 + strlen(filename)];

因为strlen() 不包括终止nul 字节,您的代码仍然非常危险,因为filename 可能是NULL,在这种情况下,因为您从不检查,所以调用时会发生未定义的行为strlen().

这个

source[++newLen] = '\0'; /* Just to be safe. */

证明您知道字符串末尾需要'\0'

所以你分配的内存比需要的少,因此问题,顺便说一句 sizeof(char) == 1 定义。

当你尝试通过输出检查文件名时,这样做

printf("*%s*\n", nameFile);

如果输出是这样的,例如

*this_is_the_filename.txt
*

表示fgets()读取的'\n'还在缓冲区中,所以需要删除,使用fgets()时可以这样做

size_t length;

fgets(userInput, sizeof(userInput), stdin);

length = strlen(userInput);
if (userInput[userInput - 1] == '\n')
    userInput[userInput - 1] = '\0';

避免在字符串末尾出现'\n'

【讨论】:

    【解决方案2】:

    一个问题:

    char nameFile[strlen(filename)];
    

    应该是:

    char nameFile[strlen(filename) + 1];
    

    另一个潜在问题:

    fgets(userInput,sizeof(userInput),stdin);
    

    在 userInput 的末尾嵌入任何行终止符(例如 - *nix 上的“\n”,Windows 上的“\r\n”)。这可能是您无法打开文件的原因。试试:

    if (NULL == fgets(userInput,sizeof(userInput),stdin)) { /* handle error */ }
    for (i = strlen(userInput); i > 0 && isspace(userInput[i]); --i);
    userInput[i] = '\0';
    readFile(userInput);
    

    此外,您可能会从以下方面获得有关 fopen 失败原因的更多信息:

    perror("File Doesnt Exist. :( \n");
    

    【讨论】:

    • 在 c 中它总是 '\n' 只有 Windows 应用程序添加 '\r\n' 来终止行。
    猜你喜欢
    • 2015-07-28
    • 2021-09-01
    • 2011-02-21
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    相关资源
    最近更新 更多