Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.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 为什么我的文件指针总是空的,即使里面有文本?_C_Gcc_Clion - Fatal编程技术网

C 为什么我的文件指针总是空的,即使里面有文本?

C 为什么我的文件指针总是空的,即使里面有文本?,c,gcc,clion,C,Gcc,Clion,我在用C打开和解析一个文件时遇到问题。现在我正在尝试打开一个文件,然后打印当前行,直到程序到达文件的末尾。但是,文件指针始终返回NULL,并且不读取文件 这是我的职责: int file_parse() { char filename[100]; printf("Enter the filename: "); scanf("%s", filename); //Get filename printf("\nYou want to encrypt or decrypt

我在用C打开和解析一个文件时遇到问题。现在我正在尝试打开一个文件,然后打印当前行,直到程序到达文件的末尾。但是,文件指针始终返回NULL,并且不读取文件

这是我的职责:

int file_parse() {
    char filename[100];
    printf("Enter the filename: ");
    scanf("%s", filename); //Get filename
    printf("\nYou want to encrypt or decrypt with file: %s", filename); //Confirm filename

    int origin[27];
    int final[27];

    FILE *fp;

    if ((fp = fopen(filename, "r")) != NULL) {
        char current_line[250];

        while (!feof(fp)) {
            fgets(current_line, 250, fp);
            printf("The current line is: %s", current_line);
        }

        fclose(fp);

    } else {
        printf("\nError! Cannot open the specified file.");
        exit(EXIT_FAILURE);
    }
}

相关:如果
fp
为空,则说明您没有提供正确的文件名。请尝试使用绝对路径名,该程序可能没有在您期望的目录中运行。打开文件的方式没有问题,因此问题必须出在文件名上。该程序是在不同的目录中执行的@巴尔马的假设是正确的。