文件的打开及创建
#include<stdio.h>
#define INFO(a) printf("信息:");printf(a);printf("\n");
#define ERROR(a) printf("错误:");printf(a);printf("\n");
int main()
{
FILE *fp=NULL;
fp=fopen("D:\\test.txt","rt");
if(fp==NULL)
{
ERROR("文件text.test打开错误");
fp=fopen("D:\\test.txt","wt");
if(fp==NULL)
{
ERROR("新建文件text.test错误");
}
else
{
INFO("新建文件text.test成功");
}
}
else
{
INFO("成功,新建文件test.txt完成");
}
}
文件的关闭
#include<stdio.h>
#define INFO(a) printf("信息:");printf(a);printf("\n");
#define ERROR(a) printf("错误:");printf(a);printf("\n");
int main()
{
int i=1;
FILE *fp=NULL;
fp=fopen("D:\\hellofriends.txt","rt");
if(fp==NULL)
{
ERROR("文件hellofriends.test打开失败");
}
else
{
INFO("成功,文件hellofriends.test打开完成");
}
i=fclose(fp);
if(i==0)
{
INFO("文件关闭成功");
}
else
{
ERROR("文件关闭失败");
}
}
文件的读写
- 字符处理函数fgetc和fputc
#include<stdio.h>
#define INFO(a) printf("信息:");printf(a);printf("\n");
#define ERROR(a) printf("错误:");printf(a);printf("\n");
int main()
{
int i=1;
char c;
FILE *fp=NULL;
fp=fopen("D:\\C_language\\helloall.txt","rt");
if(fp==NULL)
{
ERROR("文件helloall.test打开失败");
return;
}
else
{
INFO("成功,文件helloall.test打开完成");
}
c=fgetc(fp);
while(c!=EOF)
{
putchar(c);
c=fgetc(fp);
}
putchar('\n');
i=fclose(fp);
if(i==0)
{
INFO("文件关闭成功");
}
else
{
ERROR("文件关闭失败");
}
}