《(8.3.1)--7.3ExamplesofFileOperation.ppt》由会员分享,可在线阅读,更多相关《(8.3.1)--7.3ExamplesofFileOperation.ppt(18页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、Examples of File OperationExamples of File Operation#include#include int main(int argc,char*argv)int temp;FILE*fpr;if(argc=1)printf(Parameter missing!n);exit(0);if(fpr=fopen(argv1,r)=NULL)printf(Can not open the source file!n);exit(0);fseek(fpr,0L,0);while(!feof(fpr)temp=fgetc(fpr);putchar(temp);fcl
2、ose(fpr);return 0;fcat.cExample:Implementation of the function of the command cat#include#include int main(int argc,char*argv)int temp;FILE*fpr;if(argc=1)printf(Parameter missing!n);exit(0);if(fpr=fopen(argv1,r)=NULL)printf(Can not open the source file!n);exit(0);fseek(fpr,0L,0);while(!feof(fpr)temp
3、=fgetc(fpr);putchar(temp);fclose(fpr);return 0;fcat.cExample:Implementation of the function of the command cat#include#include int main(int argc,char*argv)int temp;FILE*fpr;if(argc=1)printf(Parameter missing!n);exit(0);if(fpr=fopen(argv1,r)=NULL)printf(Can not open the source file!n);exit(0);fseek(f
4、pr,0L,0);while(!feof(fpr)temp=fgetc(fpr);putchar(temp);fclose(fpr);return 0;fcat.cExample:Implementation of the function of the command cat#include#include int main(int argc,char*argv)int temp;FILE*fpr;if(argc=1)printf(Parameter missing!n);exit(0);if(fpr=fopen(argv1,r)=NULL)printf(Can not open the s
5、ource file!n);exit(0);fseek(fpr,0L,0);while(!feof(fpr)temp=fgetc(fpr);putchar(temp);fclose(fpr);return 0;fcat.cExample:Implementation of the function of the command cat#include#include int main(int argc,char*argv)int temp;FILE*fpr;if(argc=1)printf(Parameter missing!n);exit(0);if(fpr=fopen(argv1,r)=N
6、ULL)printf(Can not open the source file!n);exit(0);fseek(fpr,0L,0);while(!feof(fpr)temp=fgetc(fpr);putchar(temp);fclose(fpr);return 0;fcat.cExample:Implementation of the function of the command cat#include#include int main(int argc,char*argv)int res;if(argc=1)printf(Parameter missing!n);exit(0);res=
7、remove(argv1);if(res=0)printf(deleted!n);exit(0);else if(res=-1)printf(error happend!n);exit(0);return 0;delete.cExample:Implementation of the function of deleting files#include#include int main(int argc,char*argv)int res;if(argc=1)printf(Parameter missing!n);exit(0);res=remove(argv1);if(res=0)print
8、f(deleted!n);exit(0);else if(res=-1)printf(error happend!n);exit(0);return 0;delete.cExample:Implementation of the function of deleting files#include#include int main(int argc,char*argv)int res;if(argc=1)printf(Parameter missing!n);exit(0);res=remove(argv1);if(res=0)printf(deleted!n);exit(0);else if
9、(res=-1)printf(error happend!n);exit(0);return 0;delete.cExample:Implementation of the function of deleting files#include#include int main(int argc,char*argv)int res;if(argc!=3)printf(Parameter error!n);exit(0);res=rename(argv1,argv2);if(res=0)printf(success!n);elseprintf(error!n);return 0;move.cExa
10、mple:Implementation of the function of the command mv#include#include int main(int argc,char*argv)int res;if(argc!=3)printf(Parameter error!n);exit(0);res=rename(argv1,argv2);if(res=0)printf(success!n);elseprintf(error!n);return 0;move.cExample:Implementation of the function of the command mv#includ
11、e#include int main(int argc,char*argv)char c;FILE*fp1,*fp2;if(argc=2)printf(Parameter missing!n);exit(0);if(fp1=fopen(argv1,rb)=NULL)printf(Can not open the source file!n);exit(0);if(fp2=fopen(argv2,a+)=NULL)printf(Can not establish the output file!n);exit(0);fseek(fp1,0L,0);fseek(fp2,0L,0);while(!f
12、eof(fp1)fread(&c,1,1,fp1);fwrite(&c,1,1,fp2);fclose(fp1);fclose(fp2);return 0;copy.cExample:Implementation of the function of the command cp#include#include int main(int argc,char*argv)char c;FILE*fp1,*fp2;if(argc=2)printf(Parameter missing!n);exit(0);if(fp1=fopen(argv1,rb)=NULL)printf(Can not open
13、the source file!n);exit(0);if(fp2=fopen(argv2,a+)=NULL)printf(Can not establish the output file!n);exit(0);fseek(fp1,0L,0);fseek(fp2,0L,0);while(!feof(fp1)fread(&c,1,1,fp1);fwrite(&c,1,1,fp2);fclose(fp1);fclose(fp2);return 0;copy.cExample:Implementation of the function of the command cp#include#incl
14、ude int main(int argc,char*argv)char c;FILE*fp1,*fp2;if(argc=2)printf(Parameter missing!n);exit(0);if(fp1=fopen(argv1,rb)=NULL)printf(Can not open the source file!n);exit(0);if(fp2=fopen(argv2,a+)=NULL)printf(Can not establish the output file!n);exit(0);fseek(fp1,0L,0);fseek(fp2,0L,0);while(!feof(fp
15、1)fread(&c,1,1,fp1);fwrite(&c,1,1,fp2);fclose(fp1);fclose(fp2);return 0;copy.cExample:Implementation of the function of the command cp#include#include int main(int argc,char*argv)char c;FILE*fp1,*fp2;if(argc=2)printf(Parameter missing!n);exit(0);if(fp1=fopen(argv1,rb)=NULL)printf(Can not open the so
16、urce file!n);exit(0);if(fp2=fopen(argv2,a+)=NULL)printf(Can not establish the output file!n);exit(0);fseek(fp1,0L,0);fseek(fp2,0L,0);while(!feof(fp1)fread(&c,1,1,fp1);fwrite(&c,1,1,fp2);fclose(fp1);fclose(fp2);return 0;copy.cExample:Implementation of the function of the command cp#include#include in
17、t main(int argc,char*argv)char c;FILE*fp1,*fp2;if(argc=2)printf(Parameter missing!n);exit(0);if(fp1=fopen(argv1,rb)=NULL)printf(Can not open the source file!n);exit(0);if(fp2=fopen(argv2,a+)=NULL)printf(Can not establish the output file!n);exit(0);fseek(fp1,0L,0);fseek(fp2,0L,0);while(!feof(fp1)frea
18、d(&c,1,1,fp1);fwrite(&c,1,1,fp2);fclose(fp1);fclose(fp2);return 0;copy.cExample:Implementation of the function of the command cp#include#include int main(int argc,char*argv)char c;FILE*fp1,*fp2;if(argc=2)printf(Parameter missing!n);exit(0);if(fp1=fopen(argv1,rb)=NULL)printf(Can not open the source f
19、ile!n);exit(0);if(fp2=fopen(argv2,a+)=NULL)printf(Can not establish the output file!n);exit(0);fseek(fp1,0L,0);fseek(fp2,0L,0);while(!feof(fp1)fread(&c,1,1,fp1);fwrite(&c,1,1,fp2);fclose(fp1);fclose(fp2);return 0;copy.cExample:Implementation of the function of the command cpEmbedded Software DesignEmbedded Software Design