C语言读写文件

C语言读写文件

博客分类:  C/C++
C C++ linux 
1、读写文件,这种是把内容一次copy到内存里。
C代码   收藏代码
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. /** 
  4.  *读文件 
  5.  */  
  6. int rFile(){      
  7.     FILE *fp;  
  8.     int flen;  
  9.     char *p;  
  10.     /* 以只读方式打开文件 */  
  11.     if((fp = fopen ("e:\\1.txt","r"))==NULL)  
  12.     {  
  13.         printf("\nfile open error\n");  
  14.         exit(0);  
  15.     }  
  16.     fseek(fp,0L,SEEK_END); /* 定位到文件末尾 */  
  17.     flen=ftell(fp); /* 得到文件大小 */  
  18.     p=(char *)malloc(flen+1); /* 根据文件大小动态分配内存空间 */  
  19.     if(p==NULL){  
  20.         fclose(fp);  
  21.         return 0;  
  22.     }  
  23.     fseek(fp,0L,SEEK_SET); /* 定位到文件开头 */  
  24.     fread(p,flen,1,fp); /* 一次性读取全部文件内容 */  
  25.     p[flen]=0; /* 字符串结束标志 */  
  26.     printf("%s",p);  
  27.     fclose(fp);  
  28.     free(p);  
  29.     getch();  
  30.     return 0;  
  31. }  
  32. /** 
  33.  *写文件 
  34.  */  
  35. int wFile(){  
  36.     FILE *stream;  
  37.     stream = fopen("e:\\1.txt""w+");  
  38.     fprintf(stream, "hello world!");  
  39.     printf("The file pointer is at byte \%ld\n", ftell(stream));  
  40.     fclose(stream);  
  41.     getch();  
  42.     return 0;  
  43. }  
  44.   
  45. int main(void){  
  46.     /*rFile();*/  
  47.     wFile();  
  48.     return 0;  
  49. }  


2、按行读。 

Java代码   收藏代码
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. #define  LineSize  80 /*定义存储一行字符数据的长度*/  
  5. #define  Line     10  
  6. /** 
  7.  *读取一行,并返回存放字符串的首地址。 
  8.  */  
  9. char *ReadData_(char *buff,FILE *fp){  
  10.     return fgets(buff,LineSize,fp);  
  11. }  
  12.   
  13. int main()  
  14. {  
  15.   
  16.     ReadFile();  
  17.     /* int i=0; 
  18.     for(i;i<Line;i++){//读n行 
  19.         printf("%s\n",buff); 
  20.         p=ReadData_(buff,fp);//读取下一行。 
  21.     } 
  22.     */  
  23.   
  24.     return 0;  
  25. }  
  26. /** 
  27.  *按行读文件。 
  28.  */  
  29. void ReadFile(){  
  30.     FILE *fp=NULL;  
  31.     char *buff,*p;  
  32.     const char *filePath="E:/工行网银读不了U盾.txt";  
  33.   
  34.   
  35.     if((fp=fopen(filePath,"r"))==NULL){//判断是否能打开指定文件。  
  36.         printf("Conn't open File:[%s]",filePath);  
  37.         exit(0);  
  38.     }  
  39.     buff=(char *)malloc(LineSize*sizeof(char));//给指针分配内存。  
  40.   
  41.     while(ReadData_(buff,fp)){//读取数据。直到读到文件结尾(也就是说,不能返回字符地址了。)  
  42.         printf("%s\n",buff);  
  43.     }  
  44.     fclose(fp);  
  45. }  



3、读二进制数据。 
C代码   收藏代码
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. /** 
  5.  * function:二进制文件的复制。 
  6.  * parameter:from 源文件路径 
  7.  *          :to   目标文件路径 
  8.  * author:leson 
  9.  * date:2011-12-5 
  10.  * 
  11.  */  
  12. int copyFile(const char* from,const char* to){  
  13.     FILE *in,*out;  
  14.     int flen;  
  15.     char *p;  
  16.     /** 
  17.      *读文件 
  18.      */  
  19.     if((in = fopen (from,"rb"))==NULL){  
  20.         printf("\nfile open error:1\n");  
  21.         return 1;  
  22.     }  
  23.     fseek(in,0L,SEEK_END); /* 文件指针定位到文件末尾 */  
  24.     flen=ftell(in); /* 得到文件大小 */  
  25.     p=(char *)malloc(flen+1); /* 根据文件大小动态分配内存空间--经典*/  
  26.     if(p==NULL){  
  27.         fclose(in);  
  28.         return 2;  
  29.     }  
  30.     fseek(in,0L,SEEK_SET); /* 定位到文件开头 */  
  31.     fread(p,flen,1,in); /* 一次性读取全部文件内容 */  
  32.     p[flen]=0; /* 字符串结束标志 */  
  33.     /*printf("%s",p);*/  
  34.   
  35.     /** 
  36.      * 写文件 
  37.      */  
  38.     if((out=fopen(to,"wb"))==NULL){  
  39.         printf("\nFile open error:3");  
  40.         return 3;  
  41.     }  
  42.     fwrite(p,flen,1,out);/*往文件里写*/  
  43.     fclose(out);  
  44.     fclose(in);  
  45.     free(p);  /*动态分配的内存一定要free*/  
  46.     return 0;  
  47. }  
  48.   
  49. int main(void){  
  50.     int r=99;  
  51.     if((r=copyFile("d:\\down.txt","d:\\up.txt"))!=0){  
  52.         printf("%d",r);  
  53.     }  
  54.     getch();  
  55.     return 0;  
  56.   
  57. }  



4、 linux 的三种文件 
   ①:stdin(标准输入流) 
   ②:stdout(标准输出流) 
   ③:stderr(标准错误流) 
   
   这三种文件,在程序运行时默认提供。 

C代码   收藏代码
  1. #include <stdio.h>  
  2.   
  3. int main(int argc, char const *argv[])  
  4. {  
  5.     int input;  
  6.     // scanf("这是封装标准输入流文件的形式后的;%d",&input);  
  7.     fscanf(stdin,"%d",&input);  
  8.   
  9.     if (input<0){  
  10.         // printf("这是封装fsprintf() 后的方式\n");  
  11.         fprintf(stderr,"这是运用了标准错误流文件的形式。\n outerr=输入不能为负数。\n" );  
  12.         return 1;  
  13.     }else{  
  14.         // printf("output=%d\n",input);  
  15.         fprintf(stdout,"这是用了标准输出流文件的形式。\n output=%d\n",input);  
  16.     }  
  17.   
  18.     int i=0;  
  19.     for (i = 0; i < argc; ++i){  
  20.         fprintf(stdout, "arguments counter:[%d];arguments vector:[%s];\n",i,argv[i]);  
  21.     }  
  22.   
  23.     return 0;  
  24. }  

猜你喜欢

转载自blog.csdn.net/zhuxianxin0118/article/details/72866444