#include "stdio.h"
#include "stdlib.h"
#include "string.h"
char* main01()
{
//写文件操作
char* FileName="c:/1.txt";
char ch[]="\n\r";
FILE* fp=NULL;
fp=fopen(FileName,"wb");
if(fp == NULL)
{
printf("open file error:\n");
}
fputc('c',fp); //写一个字符
fputs("abcdefg",fp);//写一个字符串
fwrite(ch,2,1,fp); //写入时换行
fprintf(fp,"\n\r"); //写入时换行
fputs("hijklmn\n",fp);
fclose(fp);
return FileName;
}
void main03(char* file_name)
{
//fgets读文件操作
char* FileName=file_name;
char buf[100];
FILE* fp=NULL;
fp=fopen(FileName,"r");
if(fp == NULL)
{
printf("Open file error:\n");
}
while( !feof(fp))
{
printf("%s",fgets(buf,100,fp));
}
fclose(fp);
system("pause");
}
void main()
{
char* File_Name=main01();
main03(File_Name);
system("pause");
}
运行结果:
cabcdefg
hijklmn
(null)请按任意键继续. . .
(null)是指什么意思?我再文件里没有写入“(null)”