《用格式化(fprintf和fscanf函数)的方式读写文件》

//用格式化(fprintf和fscanf函数)的方式读写文件

【用格式化的方式向文件中写入数据】
#include<stdio.h>
#include<stdlib.h>

int main()
{
int i=12,f=3;
FILE *fp;


if((fp=fopen("f:\FILE_1\file_4.txt","w"))==NULL)
{
printf("can't open file ");
exit(0);
}


fprintf(fp,"%d,%d",i,f);
fclose(fp);
printf(" ");
return 0;
}

【用格式化的方式从文件中读入数据】

#include<stdio.h>
#include<stdlib.h>

int main()
{
int i=12,f=3;
FILE *fp;
if((fp=fopen("f:\FILE_1\file_4.txt","w"))==NULL)
{
printf("can't open file ");
exit(0);
}
//fprintf(fp,"%d,%d",i,f);
fscanf(fp,"%d,%d",i,f);

//把读到的数据显示在屏幕上
printf("%d,%d",i,f);
fclose(fp);
printf(" ");
return 0;
}

原文地址:https://www.cnblogs.com/sun-/p/4811376.html