1。将一个随机数写到一个文本文件中defineCRTSECURENOWARNINGSincludestdio。hincludestring。hincludestdlib。hincludetime。h将随机数写到文本文件中,并给随机数排序intmain0601(){srand((unsignedchar)time(NULL));FILEpfopen(F:a。txt,w);if(p){for(inti0;i100;i){intseqrand()256;charbuf〔100〕{0};sprintf(buf,d,seq);将证书转换为字符串写到buf中fputs(buf,p);将buf写到p中}fclose(p);}} 2。给这个文本文件中的随机数排序defineCRTSECURENOWARNINGSincludestdio。hincludestring。hincludestdlib。hintmain0602(){FILEpfopen(F:a。txt,r);intarr〔100〕{0};intindex0;while(!feof(p)){charbuf〔1024〕{0};fgets(buf,sizeof(buf),p);arr〔index〕atoi(buf);index;}fclose(p);BubbleSort(arr,100);pfopen(F:a。txt,w);for(inti0;i100;i){charbuf〔1024〕{0};sprintf(buf,d,arr〔i〕);fputs(buf,p);}fclose(p);return0;} 3。给这个任意大小的文本文件排序defineCRTSECURENOWARNINGSincludestdio。hincludestring。hincludestdlib。h任意大小的文件排序intmain0603(){FILEpfopen(F:a。txt,r);intarr〔256〕{0};while(!feof(p)){charbuf〔1024〕{0};fgets(buf,sizeof(buf),p);intaatoi(buf);arr〔a〕;统计相同数字出现的次数}close(p);pfopen(F:a。txt,w);for(inti0;i256;i){for(intj0;iarr〔i〕;j){charbuf〔100〕{0};sprintf(buf,d,i);fputs(buf,p);}}fclose(p);return0;} 4。给文本文件中的内容解析出来并求值 如:205 153defineCRTSECURENOWARNINGSincludestdio。hincludestring。hincludestdlib。h将文本中的数学表达式求值并保存到文件staticintfunc1(inta,charb,intc){switch(b){case:returnac;case:returnac;case:returnac;case:returnac;}return0;}defineNUM100intmain0701(){FILEpfopen(F:a。txt,r);chararr〔100〕〔100〕{0};。chararrcalloc(NUM,sizeof(char));chartemparr;代表当前需要写入的字符位置intindex0;while(1){charbuf〔100〕{0};fgets(buf,sizeof(buf),p);if(feof(p))break;inta0;charb0;intc0;sscanf(buf,dcd,a,b,c);sprintf(arr〔index〕,dcdd,a,b,c,func1(a,b,c));arrrealloc(arr,NUM(index2));temparr(NUM(index1));arr永远指向堆内存首地址,temp每次向后移动100字节index;}fclose(p);pfopen(a。txt,w);temparr;让temp回到起始位置for(inti0;iindex;i){fputs(arr〔index〕,p);tempNUM;}fclose(p);return0;} 5。fprintf、和fscanfdefineCRTSECURENOWARNINGSincludestdio。hincludestring。hincludestdlib。hintfunc1(inta,charb,intc){switch(b){case:returnac;case:returnac;case:returnac;case:returnac;}return0;}intmain0801()fscanf和fprintf{FILEpfopen(F:a。txt,r);FILEp1fopen(F:b。txt,w);while(1){inta0;charb0;intc0;fscanf(p,dcd,a,b,c);从文件中读取内容if(feof(p))break;fprintf(p1,dcdd,a,b,c,func1(a,b,c));将输出结果放到p的文件中}fclose(p);fclose(p1);return0;}intmain0802(){FILEpfopen(F:a。txt,r);while(1){charbuf〔100〕{0};intage0;fscanf(p,年龄d,age);fgets(buf,sizeof(buf),p);if(feof(p))break;sscanf(buf,年龄d,age);printf(d,age);}close(p);return0;}从一个文本文件中读取姓名和年龄文本文件内容如:姓名黎明,年龄45intmain0803(){FILEpfopen(F:a。txt,r);while(1){charbuf〔1042〕{0};fgets(buf,sizeof(buf),p);if(feof(p))break;charsstrtok(buf,,);charnamestrtok(buf,);printf(s,name〔1〕);sstrtok(NULL,,);printf(s,s〔5〕);printf(d,atoi(s〔7〕));}fclose(p);return0;} 5。给文本文件中成员年龄第二大的人输出defineCRTSECURENOWARNINGSincludestdio。hincludestring。hincludestdlib。h从一个文本文件中读取姓名和年龄并且给该成员排序,输出年龄第二大的人intmain0901()输出第二大的数字{inta〔10〕{55,25,11,88,99,78,52,63,87,5};intmax0;最大的数intsmax0;第二大的数for(inti0;i10;i){if(a〔i〕max){smaxmax;maxa〔i〕;}elseif(a〔i〕maxa〔i〕smax){smaxa〔i〕;}}printf(d,smax);return0;}intmain0902(){FILEpfopen(f:a。txt,r);intmax0;intsmax0;charmaxname〔100〕{0};charsmaxname〔100〕{0};while(1){charbuf〔1024〕{0};fgets(buf,sizeof(buf),p);if(feof(p))break;charsstrtok(buf,,);charnamestrtok(buf,);sstrtok(NULL,,);if(atoi(s〔5〕max)){smaxmax;maxatoi(s〔5〕);strcpy(smaxname,maxname);strcpy(maxname,name〔1〕);}elseif(atoi(s〔5〕maxatoi(s〔5〕))smax){smaxatoi(s〔5〕);strcpy(smaxname,name〔1〕);}}fclose(p);printf(s,smaxname);return0;} 6。stat函数includesysstat。hincludeunistd。hincludesystypes。hintstat(constcharfilename,structstatbuf); stat。atsize;文件大小,单位:字节 函数的参数1代表文件名,参数2是structstat结构。 函数说明:通过文件名filename获取文件信息,并保存在buf所指的结构体stat中。 返回值:执行成功则返回0,失败返回1,错误代码存于errno。 错误代码: ENOENT 参数filename指定的文件不存在 ENOTDIR 路径中的目录存在但却非真正的目录 ELOOP 欲打开的文件有过多符号连接问题,上限为16符号连接 EFAULT 参数buf为无效指针,指向无法存在的内存空间 EACCESS 存取文件时被拒绝 ENOMEM 核心内存不足 ENAMETOOLONG 参数filename的路径名称太长structstat{devtstdev;文件的设备编号inotstino;节点modetstmode;文件的类型和存取的权限nlinktstnlink;连到该文件的硬连接数目,刚建立的文件值为1uidtstuid;用户IDgidtstgid;组IDdevtstrdev;(设备类型)若此文件为设备文件,则为其设备编号offtstsize;文件字节数(文件大小)unsignedlongstblksize;块大小(文件系统的IO缓冲区大小)unsignedlongstblocks;块数timetstatime;最后一次访问时间timetstmtime;最后一次修改时间timetstctime;最后一次改变时间(指属性)};includestdio。hincludesystypes。hincludesysstat。hintmain0101(){structstatst{0};stat(F:a。txt,st);调用完stat函数后,st里得到文件属性intsizest。stsize;得到文件大小printf(d,size);return0;} 7。fread和fwrite函数 对文件格式化读写函数fprintf与fscanf而言,尽管它可以从磁盘文件中读写任何类型的文件,即读写的文件类型可以是文本文件、二进制文件,也可以是其他形式的文件。但是,对二进制文件的读写来说,考虑到文件的读写效率等原因,还是建议尽量使用fread和fwrite函数进行读写操作。 sizetfread(voidbuf,sizetsize,sizetcount,FILEfp); sizetfwrite(constvoidbuf,sizetsize,sizetcount,FILEfp); 参数1:buf内存地址;参数2:每次读取字节数; 参数3:读取次数;参数4:fopen返回的文件指针 即fread函数从文件p中读出sizecount个字节保存到buf中,而fwrite把buf中的sizecount个字节写到文件fp中。 fgetsfputsfprintf,fscanf这些函数都是针对文本文件读写的,不能对一个二进制文件进行读写。 除了文本文件之外的文件都是二进制,比如图像,可执行程序,音乐,这些均为二进制文件。defineCRTSECURENOWARNINGSincludestdio。hfwrite将一个整数写进二进制文件intmain0201(){FILEpfopen(F:a。dat,w);inta546565321;fwrite(a,1,sizeof(int),p);往文件p里写4字节内容,内容是100fwrite(a,sizeof(int),1,p);写1个整数同上fclose(p);return0;}intmain0202(){FILEpfopen(f:a。dat,r);while(1){inta0;fread(a,1,sizeof(int),p);if(feof(p))break;printf(d,a);}fclose(p);return0;} 8。fopen的a、b模式defineCRTSECURENOWARNINGSincludestdio。hincludestdlib。hincludestring。hfopena追加模式intmain0301(){FILEpfopen(f:a。txt,a);fputs(helloworld,p);fclose(p);return0;}fopenrb读写打开一个二进制文件,允许读写数据,文件必须存在windows所有的文本文件均以r为结尾,而不是结尾如果读文件时参数是r,那么系统会将前的自动隐藏若在参数r后添加b,即rb那么r会显示出来intmain0302(intargc,charargv){FILEpfopen(argv〔1〕,wb);chara〔100〕{helloworld};fputs(a,p);close(p);return0;} 9。二进制文件的拷贝defineCRTSECURENOWARNINGSincludestdio。hincludestdlib。hincludesysstat。hincludestring。h二进制文件的拷贝defineNUM10246464kintmain0401(intargc,charargv){if(argc3)return0;FILEpfopen(argv〔1〕,rb);if(pNULL)return0;FILEp1fopen(argv〔2〕,wb);if(p1NULL)return0;unsignedintindex0;统计循环多少次structstatst{0};stat(argv〔1〕,st);intsizest。stsize;得到文件大小if(sizeNUM)如果文件小于64k就开辟64k若大于则根据实际大小开辟sizeNUM;charbufmalloc(size);根据文件大小,动态分配内存while(!feof(p)){chara〔1024〕{0};intresfread(buf,1,size,p);fwrite(buf,1,res,p1);index;}free(buf);fclose(p);fclose(p1);printf(u,index);输出循环次数return0;} 10。fseek和ftell函数 intfseek(FILEstream,longintoffset,intwhence) stream这是指向FILE对象的指针,该FILE对象标识了流。 offset这是相对whence的偏移量,以字节为单位。 whence这是表示开始添加偏移offset的位置。它一般指定为下列常量之一: SEEKSET 文件的开头 SEEKCUR 文件指针的当前位置 SEEKEND 文件的末尾 返回值:如果成功,则该函数返回零,否则返回非零值。 注意:超出文件末尾,返回值为0,往回偏移超出首位置返回值还是0。 ftell()函数用来获取文件读写指针的当前位置。 其原型为:longftell(FILEstream); 返回值:成功则返回当前的读写位置,失败返回1。defineCRTSECURENOWARNINGSincludestdio。hincludestdlib。hincludesysstat。hincludestring。hfseek函数任意改变文件指针的位置SEEKSET开始SEEKCUR当前SEEKEND末尾intmain0501(){FILEpfopen(f:a。dat,w);chara〔10〕{1,2,3,4,5,6,7,8,9,10};fwrite(a,1,sizeof(a),p);fclose(p);return0;}ftell函数,功能是告诉当前指针的位置通过ftell得到文件大小intmain0502(){FILEpfopen(f:a。dat,r);通过ftell得到文件大小fseek(p,0,SEEKEND);intsizeftell(p);printf(sized,size);size即文件大小fseek(p,0,SEEKSET);再将指针放到初始位置fseek(p,2,SEEKSET);将文件指针从开始位移2个指针while(!feof(p))for(inti0;i2;i){chara〔2〕;fread(a,1,sizeof(a),p);fseek(p,2,SEEKCUR);从当前位置退回位移2个字节fseek(p,0,SEEKSET);回到文件开始fseek(p,0,SEEKEND);到达文件末尾printf(d,d,a〔0〕,a〔1〕);intlocftell(p);获取当前指针位置printf(locd,loc);}fclose(p);return0;}利用fseek生成大文件intmain0503(){FILEpfopen(f:a。dat,w);for(inti0;i100;i){chara0;fwrite(a,1,sizeof(a),p);}fclose(p);return0;}利用fseek快速生成大文件intmain0504(){FILEpfopen(f:a。dat,w);fseek(p,1000,SEEKSET);chara0;fwrite(a,1,sizeof(a),p);fclose(p);return0;}通过ftell得到文件大小intmain(intargc,charargs){if(argc2)return0;if(pNULL)return0;FILEpfopen(f:a。dat,r);intresfseek(p,0,SEEKEND);printf(d,res);intsizeftell(p);printf(sized,size);fclose(p);return0;}