为什么在 C 中给出双文件扩展名时我的程序只读取文件名?

Why does my program only read the file name when a double file extension is given in C?

#include<stdio.h>                    //standard input output library
#include<stdlib.h>

void main()
{
    FILE  *fp ;                      //fp is the file pointer
    char  ch ;                      //a variable to print contents of the file
    fp = fopen ( "text.txt", "r" ) ;//Normal file name with extension
    while ( 1 )
    {
    ch = fgetc(fp) ;
    if (ch == EOF)                     //if program reaches, EOF, it stops
        break ;
    printf ( "%c", ch);
    }
    fclose ( fp );
}

上面的程序没有读取我的文件,但是当我如下所示替换文件名时,它起作用了。

fp = fopen("text.txt.txt", "r");//双文件扩展名

为什么 text.txt 不起作用但 text.txt.txt 起作用?

这肯定意味着您的文件实际命名为 text.txt.txt

我推测您的文件浏览器配置为隐藏文件扩展名:如果文件名为 text.txt,那么您的文件浏览器可能只显示 text,因为它被命名为 text.txt.txt,您的文件浏览器显示 text.txt。 (如果我没记错的话,此配置是 Windows Explorer 在 Windows 的所有最新版本中的默认设置,所以我猜这就是你正在使用的;但这不会让我感到惊讶如果有其他文件浏览器可以这样配置的话。)