具有多个句点的C文件名

时间:2018-12-12 23:01:57

标签: c fopen

一个简单的问题。

当我尝试打开一个名为text.txt的文件时,它可以正常工作。

但是,如果我将文件重命名为text.cir.txt,则会出现错误。

该如何解决?

FILE *fd;
char nome_fich[] = "text.cir.txt";
int x;
fd = fopen("text.cir.txt", "r");

if (fd == NULL)
{
    printf("ERROR");
}
else
{
    while ((x = fgetc(fd)) != EOF)
    {
        printf("%c", x);
    }
    fclose(fd);
}

1 个答案:

答案 0 :(得分:1)

以下建议的代码:

  1. 干净地编译
  2. 执行所需的功能
  3. 正确检查并处理错误

现在,建议的代码:

#include <stdio.h>    // FILE, fopen(), perror(), printf()
#include <stdlib.h>   // exit(), EXIT_FAILURE

int main( void )
{
    FILE *fd = fopen( "text.cir.txt", "r" );

    if ( !fd )
    {
        perror( "fopen failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    int x;
    while ((x = fgetc(fd)) != EOF)
    {
        printf("%c", x);
    }
    fclose(fd);
}

针对任何.txt文件运行时,它将执行所需的操作。

注意:我正在运行Linux版本18.04