我只是在创建一个基本的文件处理程序。 代码是这样的:
#include <stdio.h>
int main()
{
FILE *p;
p=fopen("D:\\TENLINES.TXT","r");
if(p==0)
{
printf("Error",);
}
fclose(p);
}
这是错误,我无法创建文件尝试重新安装编译器并为文件使用不同的位置和名称但没有成功。 我使用的是 Windows 7,编译器是 Dev C++ 版本 5
最佳答案
更改 fopen(const char *filename, const char *mode)
中的 mode 参数:
p=fopen("D:\\TENLINES.TXT","r");//this will not _create_ a file
if(p==0) // ^
对此:
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.
如果您想向现有文件添加内容,可以使用“a+”作为打开模式。
See fopen() (有关更多开放模式,以及有关 fopen 系列函数的其他信息)
关于使用 fopen() 创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31811443/