Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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_File_Fopen - Fatal编程技术网

我无法用c打开文件

我无法用c打开文件,c,file,fopen,C,File,Fopen,我在d:drive中创建了一个名为abc的文本文件。我无法打开它。请告诉我怎么做 #include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp; clrscr(); fp = fopen("D:/abc.txt","r"); if(fp == NULL) { printf("\nCannot open");

我在d:drive中创建了一个名为abc的文本文件。我无法打开它。请告诉我怎么做

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
    FILE *fp;
    clrscr();
    fp = fopen("D:/abc.txt","r");
    if(fp == NULL)
    {
        printf("\nCannot open");
        getch();
        exit(1);
    }
    fclose(fp);
    getch();
}
#包括
#包括
#包括
void main()
{
文件*fp;
clrsc();
fp=fopen(“D:/abc.txt”,“r”);
如果(fp==NULL)
{
printf(“\n无法打开”);
getch();
出口(1);
}
fclose(fp);
getch();
}
如果您有输入错误,请尝试

 fp = fopen("D:\\abc.txt","r");
相反。

或者,如果文件与程序位于同一文件夹中:

 fp = fopen("abc.txt","r");
你有打字错误,试试看

 fp = fopen("D:\\abc.txt","r");
相反。

或者,如果文件与程序位于同一文件夹中:

 fp = fopen("abc.txt","r");

更正路径,它应该是“
D:\\abc.txt”

更正路径,它应该是“
D:\\abc.txt”

应该是

fp = fopen("D:\\abc.txt","r");
在路径中使用
\
,而不是在Windows中使用
/
,在转义序列中使用额外的
\

编辑

正如您对其他人的回答所评论的那样,
fp=fopen(“D:\\abc.txt”,“r”)也不工作,然后检查实际名称。您可能错误地给出了错误的名称,请检查您是否有这样的错误

(1) 打开命令提示符
(2) 使用DIR命令打印文件名:

c:\Users\name> D:
D:\> DIR
 Volume in drive D is FUN BOX
 Volume Serial Number is B48A-3CE7

 Directory of d:\

 27-02-2013  19:23                 0 abc.txt.txt
 26-02-2013  22:05    <DIR>          BOLLYWOOD MOVIES
 27-02-2013  19:31                 0 x
           2 File(s)              0 bytes
           1 Dir(s)  11,138,654,208 bytes free
c:\Users\name>D:
D:\>DIR
驱动器D中的音量是一个有趣的盒子
卷序列号为B48A-3CE7
d目录:\
27-02-2013 19:23 0 abc.txt.txt
26-02-2013 22:05宝莱坞电影
2013年2月27日19时31分
2个文件0个字节
1个目录11138654208字节可用
文件名是
abc.txt.txt
,但当您在文件夹中看到它时,扩展名不会出现,文件名看起来是
abc.txt

我是Linux用户,我通常在Windows中犯这个错误。这就是为什么。愿它能帮助你

应该是

fp = fopen("D:\\abc.txt","r");
在路径中使用
\
,而不是在Windows中使用
/
,在转义序列中使用额外的
\

编辑

正如您对其他人的回答所评论的那样,
fp=fopen(“D:\\abc.txt”,“r”)也不工作,然后检查实际名称。您可能错误地给出了错误的名称,请检查您是否有这样的错误

(1) 打开命令提示符
(2) 使用DIR命令打印文件名:

c:\Users\name> D:
D:\> DIR
 Volume in drive D is FUN BOX
 Volume Serial Number is B48A-3CE7

 Directory of d:\

 27-02-2013  19:23                 0 abc.txt.txt
 26-02-2013  22:05    <DIR>          BOLLYWOOD MOVIES
 27-02-2013  19:31                 0 x
           2 File(s)              0 bytes
           1 Dir(s)  11,138,654,208 bytes free
c:\Users\name>D:
D:\>DIR
驱动器D中的音量是一个有趣的盒子
卷序列号为B48A-3CE7
d目录:\
27-02-2013 19:23 0 abc.txt.txt
26-02-2013 22:05宝莱坞电影
2013年2月27日19时31分
2个文件0个字节
1个目录11138654208字节可用
文件名是
abc.txt.txt
,但当您在文件夹中看到它时,扩展名不会出现,文件名看起来是
abc.txt


我是Linux用户,我通常在Windows中犯这个错误。这就是为什么。愿它能帮助你

您的文件路径看起来有点奇怪。换成

fp = fopen("D:\\abc.txt","r");
这可能有用


除此之外,包括
并检查它,如果它失败了。

您的文件路径看起来有点奇怪。换成

fp = fopen("D:\\abc.txt","r");
这可能有用


除此之外,如果失败,请包括
并检查它。

下次,尝试使用perror()函数使错误更具体。 Perror()将解释错误代码,这将帮助您减少浪费时间,尝试查找错误类型

将此添加到您的代码中

if(fp == NULL)
{
perror(fp);
}
跑步时我得到了佩罗的信息

没有这样的文件或目录。 (因为我运行了程序,并试图读取一个文件,但没有先创建它)


请参阅,如果这是相同的问题,那么下次在您的案例中,请尝试使用perror()函数使错误更加具体。 Perror()将解释错误代码,这将帮助您减少浪费时间,尝试查找错误类型

将此添加到您的代码中

if(fp == NULL)
{
perror(fp);
}
跑步时我得到了佩罗的信息

没有这样的文件或目录。 (因为我运行了程序,并试图读取一个文件,但没有先创建它)


请参阅,如果这是相同的问题,在您的情况下

如果您正在使用TurboC,请将该文件放在TC的BIN目录中。
并将路径指定为fp=fopen(“abc.txt”,“r”)而不是任何其他备用路径。

如果您正在使用TurboC,请将该文件放在TC的BIN目录中。
并将路径指定为fp=fopen(“abc.txt”,“r”)而不是任何其他备用路径。

检查
errno
或使用
GetLastError
1)conio.h是非标准标头。2) main()应返回int 3)MS dos使用反斜杠而不是斜杠。4) 诊断输出应转到stderr,5),向其添加\n将使其可见。您需要在您的平台(OS)和编译器上更加具体。我刚刚用Windows7上的VisualStudio进行了验证,它也可以使用“/”(尽管这也是我的第一个想法)。您确定该文件存在且可读吗?@rippy请查看我的答案。谢谢大家。我想问题出在我的编译器(dosbox)上。请检查
errno
或使用
GetLastError
1)conio.h是一个非标准头。2) main()应返回int 3)MS dos使用反斜杠而不是斜杠。4) 诊断输出应转到stderr,5),向其添加\n将使其可见。您需要在您的平台(OS)和编译器上更加具体。我刚刚用Windows7上的VisualStudio进行了验证,它也可以使用“/”(尽管这也是我的第一个想法)。您确定该文件存在且可读吗?@rippy请查看我的答案。谢谢大家。我想问题出在我的编译器(dosbox)上。你们在哪个平台上,在哪个编译器上?MS Windows也接受“/”,您在哪个平台上,在哪个编译器上?MS Windows也接受“/”do
#include
,当
fp==NULL
时,检查
errno
中的值。参见do
#include
和when
fp==NULL