C/C++ 求一个用文件写成的小程序!
#include <stdio.h>
void main(){
char szt[128]={0};
printf("请输入字符串:\n"); scanf("%hs",szt);
FILE*fp=fopen("sav.txt","w");
fputs(szt,fp);
fclose(fp);
printf("\"%s\"已经保存到sav.txt\n", szt);
printf("\n按回车键继续,测试打开sav.txt\n", szt); getchar();
getchar(); szt[0]=0;
fp=fopen("sav.txt","r");
fgets(szt,127,fp);
fclose(fp);
printf("sav.txt保存内容为\"%s\"\n", szt);
}
/*
C Traps and Pitfalls
Andrew Koenig
Gillette,New Jersey,USA
October
Preface to the Chinese Edition
^Z
C Traps and Pitfalls
Andrew Koenig
Gillette,New Jersey,USA
October
Preface to the Chinese Edition
Press any key to continue
*/
#include <stdio.h>
#include <string.h>
#define N 15
#define M 81
int main() {
char str[N][M],tmp[M];
int i = 0;
FILE *fr,*fw;
fw = fopen("myinformation.txt","wt");
if(fw == NULL) {
printf("打开文件失败!\n");
return 1;
}
while(i < N && fgets(str[i],M,stdin)) {
fprintf(fw,"%s",str[i]);
++i;
}
fclose(fw);
fr = fopen("myinformation.txt","rt");
if(fr == NULL) {
printf("打开文件失败!\n");
return 1;
}
while(fgets(tmp,M,fr)) {
printf("%s",tmp);
}
fclose(fr);
return 0;
}
[您的采纳是我们前进的不竭动力]