Linux文件编程实例


  1. //捕获fopen调用中的错误   
  2. #include <stdio.h>   
  3. #include <errno.h>   
  4. #include <string.h>   
  5. #define MYFILE "missing.txt"   
  6. int main(  )   
  7.     {   
  8.         FILE* fin;   
  9.         fin=fopen( MYFILE,"r" );   
  10.         if( fin==(FILE*)NULL )   
  11.             {   
  12.                 printf( "%s: %s\n",MYFILE,strerror( errno ) );   
  13.                 exit( -1 );   
  14.                    
  15.             }   
  16.         fclose( fin );   
  17.         return 0;   
  18.            
  19.            
  20.     }   
  21.   
  22. /*数据的读写  
  23. 标准的I/O库提供了缓冲接口。有两个非常重要的属性:  
  24. 1、系统是分块进行读写的(一个块通常8KB)。字符I/O都写到FILE缓冲区中,  
  25.     这个缓冲区满的时候会自动写入介质;  
  26. 2、如果数据需要送到像控制台终端这样的交互设备,必须设置fflush或者非缓冲  
  27.    I/O  
  28. */  
  29. /********字符接口**********/  
  30. //fputc字符接口实例   
  31. #include <stdio.h>   
  32. #include <errno.h>   
  33. int main(  )   
  34.     {   
  35.         int i=0;   
  36.         FILE* fout;   
  37.         const char string[  ]={   
  38.             "This\r\nis a test\r\nfile.\r\n\0"  
  39.         };   
  40.         fout=fopen("inpfile.txt","w");   
  41.         if( fout==( FILE* )NULL )   
  42.         {   
  43.             printf( "%s: %s\n","inpfile.txt",strerror( errno ) );   
  44.             exit( -1 );   
  45.         }   
  46.         while( string[ i ]!=NULL )   
  47.             {   
  48.                 fputc( ( int )string[ i ],fout );   
  49.                 ++i;   
  50.             }   
  51.         fclose( fout );   
  52.         return 0;   
  53.     }   
  54. //fgetc字符接口实例   
  55. #include <stdio.h>   
  56. #include <errno.h>   
  57. int main(  )   
  58.     {   
  59.         FILE* fin;   
  60.         fin=fopen( "inpfile.txt","r" );   
  61.         if( fin==( FILE* )NULL )   
  62.             {   
  63.                 printf( "%s: %s\n","inpfile.txt",strerror( errno ) );   
  64.                 exit( -1 );   
  65.             }   
  66.         int i;   
  67.         while( (i=fgetc( fin ))&&i!=EOF )   
  68.             {   
  69.                 printf( "%c",(char)i );   
  70.                    
  71.             }   
  72.         return 0;   
  73.            
  74.     }   
  75. /*  
  76.   fputc(  )是向文件中写入数据;fgetc是从文件中读取数据  
  77.   总结:字符接口使用简单,但是效率不高,最好是在基于字符串的方法不可使用时才使用字符接口  
  78.  */  
  79. /************字符串接口************/  
  80. /*提供字符串读写函数4个。fputs和fgets是简单的字符串接口;  
  81.   fprintf和fscanf更复杂的接口,但提供了更多的功能。    
  82.  */  
  83. //相文件中写入变长字符串   
  84. #include <stdio.h>   
  85. #include <errno.h>   
  86. #include <stdlib.h>   
  87. #define LEN 80   
  88. int main(  )   
  89.     {   
  90.         char line[ LEN+1 ];   
  91.         FILE* fout;   
  92.         FILE* fin;   
  93.         //写入数据   
  94.         fout=fopen( "testfile.txt","w" );   
  95.         if( fout==( FILE* )NULL )   
  96.             {   
  97.                 printf( "%s: %s\n","testfile.txt",strerror( errno ) );   
  98.                 exit( -1 );   
  99.                    
  100.             }   
  101.            
  102.         fin=fdopen( 0,"r" ); //将fin与标准输入流绑定   
  103.         printf( "Please input some characters,end with CTRL+D:\n" );   
  104.         //CTRL+D结束输入   
  105.         while( ( fgets(line,LEN,fin) )!=NULL )   
  106.             {   
  107.                 fputs( line,fout );   
  108.                    
  109.             }   
  110.         fclose( fout );   
  111.         fclose( fin );   
  112.         //读取数据   
  113.         printf( "\nthe characters read from file is:\n" );   
  114.         char getLine[ LEN+1 ];   
  115.         fin=fopen( "testfile.txt","r" );   
  116.         if( fin==(FILE*)NULL )   
  117.             {   
  118.                 printf( "%s: %s\n","testfile.txt",strerror( errno ) );   
  119.                 exit( -1 );   
  120.             }   
  121.         fgets( getLine,LEN,fin );   
  122.         printf( "%s\n",getLine );   
  123.         fclose( fin );   
  124.            
  125.         return 0;   
  126.     }   
  127. /*fputs向文件中写入,fgets从文件中读取  
  128.   总结:要注意不可以同时读写同一个文件。  
  129.  */  
  130. //以ASCII格式输出结构体数据   
  131. #include <stdio.h>   
  132. #include <errno.h>   
  133. #define MAX_LINE 40   
  134. #define FILENAME "myfile.txt"   
  135. typedef struct  
  136. {   
  137.     int id;   
  138.     float x_coord;   
  139.     float y_coord;   
  140.     char name[ MAX_LINE+1 ];   
  141.        
  142. }MY_TYPE_T;   
  143. #define MAX_OBJECTS 3   
  144. /*Initialize an array of three objects*/  
  145. MY_TYPE_T objects[ MAX_OBJECTS ]={   
  146.     {0,1.5,8.3,"Frist-object"},   
  147.     {1,4.5,6.3,"Second-object"},   
  148.     {2,3.5,7.5,"Thrid-object"},   
  149. };   
  150. int main(  )   
  151.     {   
  152.         int i;   
  153.         FILE* fout;   
  154.         fout=fopen(FILENAME,"w" );   
  155.         if( fout==( FILE* )NULL )   
  156.             {   
  157.                 printf( "%s: %s\n",FILENAME,strerror( errno ) );   
  158.                 exit( -1 );   
  159.             }   
  160.         printf( "Writing...\n" );   
  161.         for( i=0;i<MAX_OBJECTS;++i )   
  162.             {   
  163.                 fprintf( fout,"%d %f %f %s\n",objects[ i ].id,objects[ i ].x_coord,   
  164.                          objects[ i ].y_coord,objects[ i ].name);   
  165.                    
  166.             }   
  167.         fclose( fout );   
  168.         printf("Write over\n");   
  169.         printf("Read from file\n");   
  170.         FILE* fin=fopen( FILENAME,"r" );   
  171.         if( fin==(FILE*)NULL )   
  172.             {   
  173.                 printf( "%s: %s\n",FILENAME,strerror( errno ) );   
  174.                 exit( -1 );   
  175.                    
  176.             }   
  177.         MY_TYPE_T objects;   
  178.         while( !feof(fin) )   
  179.             {   
  180.                 fscanf( fin,"%d %f %f %s\n",&objects.id,&objects.x_coord,   
  181.                         &objects.y_coord,&objects.name);   
  182.                 printf("%d %f %f %s\n",objects.id,objects.x_coord,   
  183.                        objects.y_coord,objects.name);   
  184.                    
  185.             }   
  186.         fclose( fin );   
  187.         return 0;   
  188.     }   
  189. /**************二进制文件的读写*****************/  
  190. /*使用fwirte、fread进行二进制文件的读写  
  191.   读写二进制文件的时候,要注意的重要问题是可移植性和字序问题。  
  192.   Intel采用小字节序,PowerPC和网络采用大字节序  
  193.  */  
  194. #include <stdio.h>   
  195. #include <errno.h>   
  196. #define MAX_LINE 40   
  197. #define FILENAME "myfile.bin"   
  198. typedef struct  
  199. {   
  200.     int id;   
  201.     float x_coord;   
  202.     float y_coord;   
  203.     char name[ MAX_LINE+1 ];   
  204.        
  205. }MY_TYPE_T;   
  206. #define MAX_OBJECTS 3   
  207. /*Initialize an array of three objects*/  
  208. MY_TYPE_T objects[ MAX_OBJECTS ]={   
  209.     {0,1.5,8.3,"Frist-object"},   
  210.     {1,4.5,6.3,"Second-object"},   
  211.     {2,3.5,7.5,"Thrid-object"},   
  212. };   
  213. int main(  )   
  214.     {   
  215.         int i;   
  216.         FILE* fout;   
  217.         fout=fopen(FILENAME,"w" );   
  218.         if( fout==( FILE* )NULL )   
  219.             {   
  220.                 printf( "%s: %s\n",FILENAME,strerror( errno ) );   
  221.                 exit( -1 );   
  222.             }   
  223.         printf( "Writing...\n" );   
  224.         fwrite( ( void* )objects,sizeof( MY_TYPE_T ),3,fout );   
  225.         fclose( fout );   
  226.         //使用fread/fseek/rewind读取二进制结构体数据   
  227.         printf( "Read from file\n" );   
  228.         MY_TYPE_T object;   
  229.         FILE* fin=fopen( FILENAME,"r" );   
  230.         if( fin==( FILE* )NULL )   
  231.             {   
  232.                 printf( "%s: %s\n",FILENAME,strerror( errno ) );   
  233.                 exit( -1 );   
  234.             }   
  235.         //定位到第三个结构体   
  236.         fseek( fin,( 2*sizeof( MY_TYPE_T ) ),SEEK_SET );   
  237.         fread( &object,sizeof( MY_TYPE_T ),1,fin );   
  238.         printf("%d %f %f %s\n",object.id,object.x_coord,   
  239.                object.y_coord,object.name);   
  240.            
  241.         fseek( fin,( 1*sizeof( MY_TYPE_T ) ),SEEK_SET );   
  242.         fread( &object,sizeof( MY_TYPE_T ),1,fin );   
  243.         printf("%d %f %f %s\n",object.id,object.x_coord,   
  244.                object.y_coord,object.name);   
  245.         //把文件指针复位到文件开头   
  246.         rewind( fin );   
  247.         fread( &object,sizeof( MY_TYPE_T ),1,fin );   
  248.         printf("%d %f %f %s\n",object.id,object.x_coord,   
  249.                object.y_coord,object.name);   
  250.         fclose( fin );   
  251.         return 0;   
  252.            
  253.     }   
  254. /************************************************/  
  255. /*  
  256.   总结:上面所有的函数都是有一些基本API来实现的。比如:open/read/write/fdopen/pread/pwrite.  
  257.   文件的输入输出API函数也可以用于管道和套接字。另外,文件和字符串操作是面向对象脚本语言的强项,如Ruby,Python.  
  258.  */  

相关内容