版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
第9章文件課后習題答案一、單選題1.B2.C3.C4.C5.B6.B7.D8.B9.A10.C二、程序填空題1.fopen(filename,"r")current_char=fgetc(file)lines++characters++prev_char=current_char2.fopen("source.txt","rb")fopen("destination.txt","wb")fgetc(source)putc(ch,destination)3.strlen(replace)strstr(temp,search)strcpy(output,line)line,searchStr,replaceStr,lineline,outputFile三、編程題1.#include<stdio.h>#include<stdlib.h>#include<string.h>voidfileread(charres[]);voidsaveToOther(charres[]);intmain(void){ charres[6]; fileread(res); printf("myfile文件中的內(nèi)容為:"); puts(res); saveToOther(res); printf("已經(jīng)成功復制myfile中內(nèi)容至yourfile!\n"); system("pause"); return0;}voidfileread(charres[]){ FILE*fp; if((fp=fopen("myfile","rb+"))==NULL) { printf("cannotopenfilemyfile!\n"); exit(0); } else { fread(res,1,6,fp); } fclose(fp);}voidsaveToOther(charres[]){ FILE*fp; if((fp=fopen("yourfile","wb+"))==NULL) { printf("cannotopenfilemyfile!\n"); exit(0); } else { fwrite(res,1,6,fp); }}2.#include<stdio.h>#include<stdlib.h>#include<string.h>#defineSIZE20voidreadByMyfile(charres[]);voidreadByYourfile(charres[]);voidcreateOurfile(charres1[],charres2[]);voidreadByOurfile(charres[]);/*其中myfile中的內(nèi)容為Hello,yourfile中的內(nèi)容為World*/intmain(void){ charend[SIZE]; charres1[SIZE],res2[SIZE]; readByMyfile(res1); readByYourfile(res2); createOurfile(res1,res2); readByOurfile(end); printf("ourfile文件中的內(nèi)容為:\n"); puts(end); system("pause"); return0;}voidreadByMyfile(charres[]){ FILE*fp; if((fp=fopen("myfile","rb+"))==NULL) { printf("cannotopenfilemyfile!\n"); exit(0); } else { fread(res,1,6,fp);/*讀取6個1個字節(jié)長度的元素*/ } fclose(fp);/*關閉文件*/}voidreadByYourfile(charres[]){ FILE*fp; if((fp=fopen("yourfile","rb+"))==NULL) { printf("cannotopenfilemyfile!\n"); exit(0); } else { fread(res,1,6,fp);/*讀取6個1個字節(jié)長度的元素*/ } fclose(fp);/*關閉文件*/}voidcreateOurfile(charres1[],charres2[]){ FILE*fp; strcat(res1,res2);/*字符串拼接*/ if((fp=fopen("ourfile","wb+"))==NULL) { printf("cannotopenfilemyfile!\n"); exit(0); } else { fwrite(res1,1,11,fp); } fclose(fp);}voidreadByOurfile(charres[]){ FILE*fp; if((fp=fopen("ourfile","rb+"))==NULL) { printf("cannotopenfilemyfile!\n"); exit(0); } else { fread(res,1,11,fp);/*讀取6個1個字節(jié)長度的元素*/ } fclose(fp);/*關閉文件*/}3.#include<stdio.h>#include<ctype.h>#include<string.h>#defineMAX_ERRORS10//假設定錯誤類型的最大種類數(shù)#defineBUFFER_SIZE100//讀取緩沖區(qū)大小typedefstruct{charcode[5];//錯誤代碼intcount;//出現(xiàn)次數(shù)}ErrorType;intmain(){FILE*file;charbuffer[BUFFER_SIZE];Errorerrors[MAX_ERRORS]={0};intindex,found;file=fopen("log.txt","r");//日志文件路徑if(file==NULL){printf("Erroropeningfile\n");return1;}while(fgets(buffer,BUFFER_SIZE,file)){//清除前后空格buffer[strspn]=0;buffer[strtrim(buffer);//查找是否匹配錯誤類型for(index=0;index<MAX_ERRORS;index++){if(strcmp(errors[index].code,buffer)==0){errors[index].count++;break;}}//新錯誤類型if(index==MAX_ERRORS){printf("Unknownerrortype:%s\n",buffer);continue;}}fclose(file);//輸出統(tǒng)計printf("ErrorType\tCount\n");for(index=0;index<MAX_ERRORS;index++)if(errors[index].count>0)printf("%s\t%d\n",errors[index].code,errors[index].count);return0;}//輔助函數(shù)去除字符串前后空格voidstrTrim(char*str){char*end;while(isspace(*str))str++;//移動到首非空格if(*str==0)return;//空串end=str+strlen(str)-1;//尾指針while(end>str&&isspace(*end))end--;//移尾空格*(end+1)=0;//置0}4.#include<stdio.h>#include<stdlib.h>//簡單行讀取整數(shù)intreadLine(FILE*file,int*num){returnfscanf(file,"%d",num);}//寫入一行voidwriteLine(intnum,FILE*file){fprintf(file,"%d\n",num);}//快速排算法voidquickSort(intarr[],intleft,intright){inti=left,j=right,pivot=arr[(left+right)/2];while(i<=j){while(arr[i]<pivot)i++;while(arr[j]>pivot)j--;if(i<=j){inttemp=arr[i];arr[i]=arr[j];arr[j]=temp;i++;j--;}}}//主函數(shù)intmain(){FILE*inputFile,*outputFile;intarray[10000],size=0,i;inputFile=fopen("input.txt","r");outputFile=fopen("sorted.txt","w");if(inputFile==NULL||outputFile==NULL){printf("Erroropeningfiles");return1;}while(!feof(inputFile))readLine(inputFile,&array[size++]);quickSort(array,size-1);for(i=0;i<size;i++)writeLine(array[i],outputFile);fclose(inputFile);fclose(outputFile);printf("Sortingcompleted.");return0;}5./*加密*/#include<stdio.h>#defineKEY3//加密鑰,可自定義#defineBUF_SIZE100intmain(){FILE*in_file,*out_file;charbuff[BUF_SIZE],ch;in_file=fopen("original.txt","r");//原文out_file=fopen("encrypted.txt","w");//密文if(in_file==NULL||out_file==NULL){printf("Erroropeningfiles.\n");return1;}while((ch=fgetc(in_file))!=EOF){ch=ch+KEY;//加密if(ch>127)ch-=128;//ASCII超限處理fputc(ch,out_file);}fclose(in_file);fclose(out_file);prin
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年口腔醫(yī)療管理公司員工薪酬福利管理制度
- 環(huán)境保護技術研發(fā)與應用手冊
- 2026年劇本殺運營公司特殊顧客群體服務制度
- 護理扎針技巧與注意事項
- 2025年新能源汽車行業(yè)技術革新趨勢研究報告
- 護理扎針的安全與衛(wèi)生
- 2026年海洋探測設備技術報告
- 信托受益權登記制度
- 2025-2026學年廣東深圳紅嶺中學九年級(上)期中考英語試題含答案
- 中醫(yī)科醫(yī)師制度
- 人教版小學數(shù)學六年級下冊第二單元《百分數(shù)》(二) 單元作業(yè)設計表
- 2024至2030年高強度快硬硫鋁酸鹽水泥項目投資價值分析報告
- 制造業(yè)企業(yè)質(zhì)量管理能力評估規(guī)范
- 13J933-2體育場地與設施(二)
- 豆制品購銷合同范本
- DL-T-710-2018水輪機運行規(guī)程
- 腰椎術后腦脊液漏護理課件
- 中建《工程預結算管理辦法》
- 鋼結構工程測量專項方案樣本
- 《叉車安全作業(yè)培訓》課件
- 基于區(qū)塊鏈的供應鏈金融平臺實施方案
評論
0/150
提交評論