#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i,j,flag;
char a[100];
FILE*fp;
fp=fopen("num.dat","w");
if(fp==NULL)
{
printf("fopen error\n");
return -1;
}
scanf("%s",a);
for(i=0; a[i]!='\0'; i++)
{
fprintf(fp,"%d ",a[i]-'0');
}
fclose(fp);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(void)
{
char str[128];
FILE*fp;
fp = fopen("str.txt","w");
if(!fp) {
printf("fopen error: %s\n", strerror(errno));
return -1;
}
scanf("%s", str);
fputs(str, fp);
puts(str);
fclose(fp);
return 0;
}
供参考~
fgets/fputs是文件读写的对应函数,fscanf/fprintf这些都是ASCII形式读写文件的,后面的两个是格式化,前面两个是一次一行;
fread/fwrite这是二进制形式读写文件