卞敏卞孝萱:如何用c语言进行文件读写,使数据保存进某一文件中?

来源:百度文库 编辑:高考问答 时间:2024/04/27 14:23:16

我这里有一个完整的实例,你看看吧!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
FILE *fp;
char str[80];
if((fp=fopen("jak1.txt","w"))==NULL)
{
printf("Cannot open file");
exit(0);
}
do
{
printf("Enter a string (Cr to quit):\n");
gets(str);
if(*str!='\n')
{
strcat(str,"\n");
fputs(str,fp);
}
}while(*str!='\n');

printf("\n\n Displaying Contents of File JAK1\n\n");
rewind(fp);
while (!feof(fp))
{
fgets(str,81,fp);
printf("\n%s",str);
}
fclose(fp);
}

不是吧?这个你也不会?

FILE *pfile;
pfile=fopen(filename,"w+");
fscanf(f, "%d ", &datatowrite);
fclose(pfile);