如何用文本档编辑c语言,c语言读写word文档
int a; char b,c[100]; int main(){ FILE * fp1 = fopen("input.txt", "r");//打開輸入文件 FILE * fp2 = fopen("output.txt", "w");//打開輸出文件 if (fp1==NULL || fp2==NULL) {//若打開文件失敗則退出 puts("不能打開文件!"); rturn 0; } fscanf(fp1,"%d",&a);//從輸入文件讀取一個整數(shù) b=fgetc(fp1);//從輸入文件讀取一個字符 fgets(c,100,fp1);//從輸入文件讀取一行字符串 printf("%ld",ftell(fp1));//輸出fp1指針當(dāng)前位置相對于文件首的偏移字節(jié)數(shù) fputs(c,fp2);//向輸出文件寫入一行字符串 fputc(b,fp2);//向輸出文件寫入一個字符 fprintf(fp2,"%d",a);//向輸出文件寫入一個整數(shù) fclose(fp1);//關(guān)閉輸入文件 fclose(fp2);//關(guān)閉輸出文件,相當(dāng)于保存 return 0; }。
c文件的寫入和讀取-C語言中怎樣讀取文件數(shù)據(jù)并對數(shù)據(jù)排序再重新寫
你好,很高興為您解答。
我來回答吧: 首先是兩個輸入文件: 一個文本文件:scoret。txt 一個二進(jìn)制文件:scoreb。
txt 文本文件scoret。txt我們可以直接編輯(按要求編輯五個學(xué)生的姓名和成績),而二進(jìn)制文件不易編輯。
我們先寫一個代碼,實(shí)再將scoret。txt文件轉(zhuǎn)成二進(jìn)制文件scoreb。
txt #include #include #include struct stu { char name[30]; int grade; }; int main() { int i,n; struct stu s[10]; FILE *fpIt,*fpIb; /*輸入*/ FILE *fpOt,*fpOb; /*輸出*/ fpIt=fopen("scoret。 txt","r"); fpIb=fopen("scoreb。
txt","wb"); if (fpIt==NULL || fpIb==NULL) { printf("File Open Error:\npress any key to exit:\n"); getch(); return -1; } i=0; while (!feof(fpIt)) { fgets(s[i]。 name,30,fpIt); /*有空格只能這樣輸入*/ fscanf(fpIt,"%d\n",&s[i]。
grade); /*文本輸入*/ printf("i=%d,name=%s\t grade=%d\n",i,s[i]。 name,s[i]。
grade); getch(); fwrite(&s[i],sizeof(struct stu),1,fpIb); i++; } n=i; printf("n=%d\n",n); fclose(fpIt); fclose(fpIb); getch(); return 0; } =============== 上面的代碼在TC或VC下編輯運(yùn)行,就可以生成scoreb。 txt文件。
這時scoreb。txt的學(xué)生信息與scoret。
txt是一樣的。 接著就要修改一下scoret。
txt文件的內(nèi)容,重新編輯5個學(xué)生的信息給scoreb。txt 這樣使得scoret。
txt與scoreb。txt分別各有五個學(xué)生的信息。
當(dāng)然你如果有原始文件的話,上面都是浮云。 下面是你這個問題的重點(diǎn): #include #include #include #include struct stu { char name[30]; int grade; }; int main() { int i,j,n; struct stu s[10],temp; FILE *fpIt,*fpIb; /*輸入*/ FILE *fpOt,*fpOb; /*輸出*/ fpIt=fopen("scoret。
txt","r"); fpIb=fopen("scoreb。txt","rb"); fpOt=fopen("scoreOt。
txt","w"); fpOb=fopen("scoreOb。txt","wb"); if (fpIt==NULL || fpIb==NULL || fpOt==NULL || fpOb==NULL) { printf("File Open Error:\npress any key to exit:\n"); getch(); return -1; } n=0; while (!feof(fpIt)) { fgets(s[n]。
name,30,fpIt); /*有空格只能這樣輸入*/ fscanf(fpIt,"%d\n",&s[n]。grade); /*文本輸入*/ printf("n=%d,name=%s\t grade=%d\n",n,s[n]。
name,s[n]。grade); getch(); n++; } fclose(fpIt); printf("===========\n"); while (!feof(fpIb) && n name,s[n]。
grade); getch(); n++; } printf("n=%d\n",n); fclose(fpIb); /*排序*/ for (i=0;i grade grade,s[i]。name); /*寫文件*/ for (i=0;i 當(dāng)然二進(jìn)制文件你如果用記事本打開的話,一般是亂碼。
你可以弄一個UltraEdit之類的文本編輯器,查看其二進(jìn)制代碼。
總結(jié)
以上是生活随笔為你收集整理的如何用文本档编辑c语言,c语言读写word文档的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。