freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

c程序設(shè)計案例教程(下)ppt-資料下載頁

2025-10-08 03:35本頁面
  

【正文】 92】 使用函數(shù) fprintf()向文本文件中寫入不同類型的數(shù)據(jù) include include include int main(void) { FILE * fp。 int i=10。 double num=。 char c=39。\n39。 char s[ ]=this is a string。 if((fp=fopen(, w)) == NULL) { printf(Can not open file \n)。 exit(1)。 } else { fprintf(fp, %s%c%d\n%lf\n, s, c, i, num)。 if(fclose(fp)) { printf(Can not close file !\n)。 exit(1)。 } else { system(type )。 } } return 0。 } 程序解析 【 例 113】 把字符串 Wele to study C program!寫入文件 。 include include int main(void) { FILE * fp。 char name[]={}。 if((fp=fopen(name, w))==NULL) { printf(Can not open file %s\n, name)。 exit(1)。 } else { fprintf(fp, %s,Wele to study C program!)。 if(fclose(fp)) { printf(Can not close file %s!\n, name)。 exit(1)。 } } return 0。 } 讀取學(xué)生基本信息文件 格式化文件讀函數(shù) fscanf 函數(shù) fscanf的原型如下: int fscanf(FILE * fp, const char * format[, argument ]...)。函數(shù)按照格式字符串 format規(guī)定的格式從文件指針 fp指向的文件中讀取多個數(shù)據(jù)存入變量 argument中。函數(shù)返回讀出的字節(jié)數(shù),當(dāng)讀操作發(fā)生錯誤時返回一個負(fù)值。函數(shù)的形式參數(shù)個數(shù)不定,第一個參數(shù) fp為文件指針;第二個 format為格式控制字符串,其中包括若干個格式控制字符。格式控制字符串 format中有幾個格式控制符,就應(yīng)該有幾個 argument參數(shù)與之配對。 【 例 94】 使用函數(shù) fscanf()從文件中讀出不同類型的數(shù)據(jù) include int main(void) { FILE * fp。 long l。 float num。 char s[81]。 char c。 if((fp=fopen(, w+))==NULL) { printf(Can not open the file!\n)。 exit(1)。 } else { fprintf(fp, %s %ld %f %c,astring, 65000, , 39。x39。)。 fseek(fp, 0L, SEEK_SET)。 fscanf(fp, %s %ld %f %c, s, amp。l, amp。num, amp。c)。 printf(%s\n%ld\n%f\n%c\n, s, l, num, c)。 fclose(fp)。 } return 0。 } 程序解析 【 例 115】 首先向文本文件 3名學(xué)生的基本信息,學(xué)生的基本信息包括學(xué)生的學(xué)號、姓名、性別、年齡和家庭地址,然后將學(xué) 生的基本信息依次讀出顯示在屏幕上。 include include int main(void) { FILE * fp。 long no。 char name[20], sex[2], address[50]。 int age。 if((fp=fopen(, w))==NULL) { printf(Can not open the file!\n)。 exit(1)。 } fprintf(fp, %ld %s %s %d %s\n, 1, 張三 , 男 , 21, 哈爾濱 )。 fprintf(fp, %ld %s %s %d %s\n, 2, 李四 , 男 , 23, 北京 )。 fprintf(fp, %ld %s %s %d %s, 3, 王五 , 男 , 22, 上海 )。 if(fclose(fp)) { printf(Can not close the file!\n)。 exit(1)。 } if((fp=fopen(, r))==NULL) { printf(Can not open the file!\n)。 exit(1)。 } printf(no|name|sex|age|address\n)。 printf(\n)。 while(!feof(fp)) { fscanf(fp, %ld %s %s %d %s, amp。no, name, sex, amp。age, address)。 printf(%ld %s %s %d %s\n, no, name, sex, age, address)。 } printf(\n)。 if(fclose(fp)) { printf(Can not close the file!\n)。 exit(1)。 } return 0。 } 復(fù)制文件 程序解析 【 例 116】 將文本文件 ,文件名為 。 include include int main(void) { FILE * fp1, * fp2。 char c。 if((fp1=fopen(, r))==NULL) { printf(Can not open file !\n)。 exit(EXIT_FAILURE)。 } if((fp2=fopen(,w))==NULL) { printf(Can not open file !\n)。 exit(1)。 } while(!feof(fp1)) { c=fgetc(fp1)。 if(c!=EOF) fputc(c,fp2)。 } fclose(fp1)。 fclose(fp2)。 return 0。 } 字符方式文件讀寫函數(shù) fputc和 fgetc 1.字符方式文件寫函數(shù) fputc() 函數(shù) fputc()的原型如下: int fputc(int c, FILE * fp)。函數(shù)將字符 c寫入文件指針 fp指向的文件中。若成功執(zhí)行,函數(shù)返回要寫的字符,否則返回 EOF。 【 例 97】 使用函數(shù) fputc()在顯示器上顯示字符串。 include int main(void) { char strptr[]=This is a test of fputc!!\n。 char * p。 p=strptr。 while((*p != 39。\039。) amp。amp。 fputc(*(p++), stdout) != EOF)。 return 0。 } 2.字符方式文件寫函數(shù) fgetc() 函數(shù) fgetc的原型如下: int fgetc( FILE * fp)。函數(shù)從文件指針 fp指向的文件中讀出一個字符。若成功執(zhí)行,函數(shù)返回讀到的字符,否則返回 EOF。 【 例 98】 使用函數(shù) fgetc()從文件中讀入 80個字符,顯示在屏幕上。 源程序見教材 P209。 字符串方式文件讀寫函數(shù) fgets和 fputs 1.字符串方式寫函數(shù) fputs() 函數(shù) fputs的原型如下: int fputs( const char * string, FILE * fp)。函數(shù)將常量字符指針 string指向的字符串寫入文件指針 fp指向的文件中。若成功執(zhí)行,函數(shù)返回讀到的字符個數(shù),否則返回 EOF。 【 例 99】 使用函數(shù) fputs將字符串顯示在顯示器上。 include int main(void) { fputs(Hello world from fputs.\n, stdout)。 return 0 。 } 2.字符串方式讀函數(shù) fgets() 函數(shù) fgets的原型如下: char * fgets(char * string, int n, FILE * fp) 。函數(shù)從文件指針 fp指向的文件中讀出 n個字符存入字符指針指向的數(shù)組中。若成功執(zhí)行,函數(shù)返回讀到的字符,否則返回 EOF。 形式參數(shù): stream為文件指針。 【 例 910】 使用函數(shù) fgets()從文件中讀入 100個字符顯示出來。 include int main(void) { FILE * fp。 char line[100]。 if((fp=fopen(, r)) != NULL) { if(fgets(line, 100, fp) == NULL) printf(fgets error \n)。 else printf(%s, line)。 fclose(fp) 。 } return 0。 } 數(shù)據(jù)塊讀寫函數(shù) fread和 fwrite 函數(shù) fwrite()的原型如下: size_t fwrite(const void * buffer, size_t size, size_t count, FILE * fp)。函數(shù)將 count個大小為 size字節(jié)的數(shù)據(jù)項(數(shù)據(jù)項的地址通過指針 buffer給出)寫入文件指針 fp指向的文件中。函數(shù)返回實際寫入文件中的數(shù)據(jù)項個數(shù)。 函數(shù) fread的原型如下: size_t fread(void * buffer, size_t size, size_t count, FILE * fp) 。函數(shù)將文件指針 fp指向的文件中 count個大小為size字節(jié)的數(shù)據(jù)項讀入指針 buffer指向的位置。函數(shù)返回實際從文件中讀到的數(shù)據(jù)項個數(shù)。 其他相關(guān)函數(shù) 1.函數(shù) feof() 用于判斷文件的結(jié)尾標(biāo)志。其調(diào)用格式為: feof(fp)。判斷 fp指針是否已經(jīng)到文件末尾,即讀文件是否讀到了文件結(jié)束的位置。該函數(shù)返回 1,表示已經(jīng)到了文件結(jié)束位置,返回 0,表示文件未結(jié)束。 2.函數(shù) rewind() 定位文件讀寫指針,使其指向讀寫文件的首地址,即打開文件時文件讀寫位置指針?biāo)赶虻奈恢谩.?dāng)訪問某個文件,進(jìn)行了文件讀寫,使文件指針指向了文件中間或末尾,又想回到文件的首地址進(jìn)行重新讀寫時,可使用該函數(shù)。其調(diào)用格式為: rewind(FILE * fp)。 fp是文件指針,指向所打開的文件。 3.函數(shù) fseek() 該函數(shù)控制指針移動。函數(shù)的調(diào)用格式為: fseek(FILE * fp, long offset, int from)。其中, fp是文件指針, offset表示偏移量,它應(yīng)是long型數(shù)據(jù),使用常量時,應(yīng)加上后綴“ L”, offset可取負(fù)值,表示按相反方向計算偏移量,即為正時表示從當(dāng)前位置向后計算,為負(fù)時從當(dāng)前位置向前計算。 from表示從哪個位置開始計算偏移量,位置可取 3種值:文件首部、當(dāng)前位置和文件尾部,實際表示時分別對應(yīng)值 0、 2,或常量 SEEK_SET、 SEEK_CUR、 SEEK_END。例如: fseek(fp, 20L, 0)。表示將文件指針移動到離文件首 20個字節(jié)位置。 4.函數(shù) ftell() 函數(shù) ftell()用來獲取當(dāng)前文件讀寫指針的位置,即相對于文件開頭的位移量。調(diào)用形式為: ftell(FILE * fp)。 5.函數(shù) ferror() 函數(shù) ferror()用來檢查文件在用各種輸入輸出函數(shù)進(jìn)行讀寫是否出錯,若返回值為 0,表示未出錯,否則表示有錯誤。 6.函數(shù) clearerr() 函數(shù) clearerr()用來清除出錯標(biāo)志和文件結(jié)束標(biāo)志,使它們?yōu)?0值。調(diào)用形式為 clearerr(FILE * fp)。 綜合應(yīng)用 ——個人收支記帳本 源程序請見教材 P213。
點擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號-1