保存键盘输入的字符串:
#include <stdio.h>
#include <string.h>
int main()
{
//写入
char ch;
FILE *f= NULL;
f=fopen("b.txt", "w+");
int count = 0;
if(f!=NULL){
printf("请输入要保存的字符:\n");
ch = getchar();
while (ch!='\n') {
count++;
fputc(ch, f);
ch = getchar();
}
printf("一共输入了%d个字符\n",count);
}
rewind(f);
//把文件指针重新指向文件开头,文件指针在正常情况下会一步一步的向下偏移
ch = fgetc(f);
printf("输入的字符为:\n");
while(ch!=EOF){
putchar(ch);
ch=fgetc(f);
}
printf("\n");
fclose(f);
return 0;
}
|
|