使用fopen()创建文件

Pop*_*eye 2 c file-handling dev-c++ null-pointer

我只是创建一个基本的文件处理程序.代码是这样的:

#include <stdio.h>
int main()
{
FILE *p;
p=fopen("D:\\TENLINES.TXT","r");
if(p==0)
{
    printf("Error",);

}

fclose(p);
}
Run Code Online (Sandbox Code Playgroud)

这是给错误,我无法创建文件尝试重新安装编译器和使用不同的位置和名称的文件,但没有成功.我使用的是Windows 7,编译器是Dev C++版本5

ryy*_*ker 7

从以下位置更改打开模式:

p=fopen("D:\\TENLINES.TXT","r");//this will not _create_ a file
if(p==0)
Run Code Online (Sandbox Code Playgroud)

对此:

p=fopen("D:\\TENLINES.TXT","w");//this will create a file for writing.
if(p==NULL)                     //If the file already exists, it will write over
                                //existing data.
Run Code Online (Sandbox Code Playgroud)

如果要将内容添加到现有文件,可以使用"a +"作为打开模式.

fopen的参考(更多开放模式,以及有关fopen函数族的其他信息)