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

Linux文件编程实例的更多相关文章

  1. linux内核模块编程实例

    linux内核模块编程实例 学号:201400814125 班级:计科141 姓名:刘建伟 1.确定本机虚拟机中的Ubuntu下Linux的版本 通过使用命令uname -a/uname -r/una ...

  2. linux文件编程----系统调用

    linux中文件编程可以使用两种方法: 1).linux系统调用 2).C语言库函数 前者依赖于linux系统,后者与操作系统是独立的. 在 linux系统中,所有打开的文件也对应一个数字,这个数字由 ...

  3. Linux 文件编程、时间编程基本函数

    文件编程 文件描述符 fd --->>>数字(文件的身份证,代表文件身份),通过 fd 可找到正在操作或需要打开的文件. 基本函数操作: 1)打开/创建文件 int open (co ...

  4. Linux多线程编程实例解析

    Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...

  5. Linux c编程实例_例子

    例一:字符与整型变量的实现 #include <stdio.h> int main() { int c1,c2; char c3; c1='a'-'A'; c2='b'-'B'; c3=; ...

  6. 多功能电子通讯录(涉及到了双向链表的使用,Linux文件编程等等)

    readme.txt //作为一个程序员,我们咋么能不写用户手册呢!MSP的我觉得用户体验是王道,苹果手机的用户体验的确不错!不过WP加油!我去,扯远了!赶紧看我的程序吧!  歡迎使用多功能電子通訊錄 ...

  7. Linux网络编程实例解析

    **************************************************************************************************** ...

  8. linux 定时器编程实例(完善中).....

    最近在写linux 下的定时器编程实验,测试发现 usleep函数在 x86 架构下的定时还是比较准确的,在arm9下 就不太准了. 今天用linux 下的setitimer()函数进行了定时 器的测 ...

  9. Linux多进程编程实例

    前言:编写多进程程序时,我们应该了解一下,创建一个子进程时,操作系统内核是怎样做的.当通过fork函数创建新的子进程时,内核将父进程的用户地址空间的内容复制给子进程,这样父子进程拥有各自独立的用户空间 ...

随机推荐

  1. 图论(网络流):[SCOI2015]小凸玩矩阵

    Description 小凸和小方是好朋友,小方给小凸一个N*M(N<=M)的矩阵A,要求小秃从其中选出N个数,其中任意两个数字不能在同一行或同一列,现小凸想知道选出来的N个数中第K大的数字的最 ...

  2. 数据结构(线段树):HDU 5649 DZY Loves Sorting

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  3. 【数学】CSU 1810 Reverse (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1810 题目大意: 一个长度为N的十进制数,R(i,j)表示将第i位到第j位翻转过来后的 ...

  4. c#反序列化

    C#序列化(转载) 2011-03-15  |  凯之风  |  转藏(2) 序列化有下列要求: A> 只序列化PUBLIC的成员变量和属性 B> 类必须有默认识构造函数 C> 如果 ...

  5. 《程序设计中的组合数学》——polya计数

    我们在高中的组合数学中常常会碰到有关涂色的问题,例如:用红蓝两种颜色给正方形的四个顶点涂色,会有几种不同的方案.在当时,我们下意识的认为,正方形的四个顶点是各不相同的,即正方形是固定的.而实际上我们知 ...

  6. G - Supermarket poj1456

    题目的描述很长,其实描述的问题很简单,说有n的商品,它们每个的价值是pi,但是呢,再过di天这些商品就不能卖了(有可能过期了...),现在给出来每个商品的价值和可以卖的最后期限,问可以得到最多多少资金 ...

  7. IT项目经理应具备的十大软技能

    现在,企业对IT部项目经理的要求越来越多.如果你认为IT项目成员只需要技术性能力,那可就错了. 据IT招聘公司调查发现,几年人们对项目管理软技能的兴趣明显浓厚起来.许多企业尽量避免把IT部门看成只是成 ...

  8. sqlserver使用户只能在某个架构下建立表和存储过程

    1.首先,建立一个用户之后,默认的架构是dbo,默认的角色是public.这种情况下,这个用户将看不到dbo以及其他架构下的对象.除非单独进行授权.   2.新建一个架构test,然后使得这个架构的所 ...

  9. python推荐淘宝物美价廉商品 2.0

    改动: 新增功能 :可选择只看天猫或淘宝 代码模块化封装,参数配置或输入单独在一个py文件管理,主函数功能只留出参数传入在setting配置的py文件里. main.py代码: # -*- codin ...

  10. jQuery Ajax 实例 具体介绍$.ajax、$.post、$.get的使用

    Jquery在异步提交方面封装的非常好.直接用AJAX非常麻烦须要处理浏览器之间的兼容问题,Jquery大大简化了我们的这些操作操作.不用在考虑浏览器这方面的问题,能够直接使用! $.post.$.g ...