Linux文件编程实例
- //捕获fopen调用中的错误
- #include <stdio.h>
- #include <errno.h>
- #include <string.h>
- #define MYFILE "missing.txt"
- int main( )
- {
- FILE* fin;
- fin=fopen( MYFILE,"r" );
- if( fin==(FILE*)NULL )
- {
- printf( "%s: %s\n",MYFILE,strerror( errno ) );
- exit( -1 );
- }
- fclose( fin );
- return 0;
- }
- /*数据的读写
- 标准的I/O库提供了缓冲接口。有两个非常重要的属性:
- 1、系统是分块进行读写的(一个块通常8KB)。字符I/O都写到FILE缓冲区中,
- 这个缓冲区满的时候会自动写入介质;
- 2、如果数据需要送到像控制台终端这样的交互设备,必须设置fflush或者非缓冲
- I/O
- */
- /********字符接口**********/
- //fputc字符接口实例
- #include <stdio.h>
- #include <errno.h>
- int main( )
- {
- int i=0;
- FILE* fout;
- const char string[ ]={
- "This\r\nis a test\r\nfile.\r\n\0"
- };
- fout=fopen("inpfile.txt","w");
- if( fout==( FILE* )NULL )
- {
- printf( "%s: %s\n","inpfile.txt",strerror( errno ) );
- exit( -1 );
- }
- while( string[ i ]!=NULL )
- {
- fputc( ( int )string[ i ],fout );
- ++i;
- }
- fclose( fout );
- return 0;
- }
- //fgetc字符接口实例
- #include <stdio.h>
- #include <errno.h>
- int main( )
- {
- FILE* fin;
- fin=fopen( "inpfile.txt","r" );
- if( fin==( FILE* )NULL )
- {
- printf( "%s: %s\n","inpfile.txt",strerror( errno ) );
- exit( -1 );
- }
- int i;
- while( (i=fgetc( fin ))&&i!=EOF )
- {
- printf( "%c",(char)i );
- }
- return 0;
- }
- /*
- fputc( )是向文件中写入数据;fgetc是从文件中读取数据
- 总结:字符接口使用简单,但是效率不高,最好是在基于字符串的方法不可使用时才使用字符接口
- */
- /************字符串接口************/
- /*提供字符串读写函数4个。fputs和fgets是简单的字符串接口;
- fprintf和fscanf更复杂的接口,但提供了更多的功能。
- */
- //相文件中写入变长字符串
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #define LEN 80
- int main( )
- {
- char line[ LEN+1 ];
- FILE* fout;
- FILE* fin;
- //写入数据
- fout=fopen( "testfile.txt","w" );
- if( fout==( FILE* )NULL )
- {
- printf( "%s: %s\n","testfile.txt",strerror( errno ) );
- exit( -1 );
- }
- fin=fdopen( 0,"r" ); //将fin与标准输入流绑定
- printf( "Please input some characters,end with CTRL+D:\n" );
- //CTRL+D结束输入
- while( ( fgets(line,LEN,fin) )!=NULL )
- {
- fputs( line,fout );
- }
- fclose( fout );
- fclose( fin );
- //读取数据
- printf( "\nthe characters read from file is:\n" );
- char getLine[ LEN+1 ];
- fin=fopen( "testfile.txt","r" );
- if( fin==(FILE*)NULL )
- {
- printf( "%s: %s\n","testfile.txt",strerror( errno ) );
- exit( -1 );
- }
- fgets( getLine,LEN,fin );
- printf( "%s\n",getLine );
- fclose( fin );
- return 0;
- }
- /*fputs向文件中写入,fgets从文件中读取
- 总结:要注意不可以同时读写同一个文件。
- */
- //以ASCII格式输出结构体数据
- #include <stdio.h>
- #include <errno.h>
- #define MAX_LINE 40
- #define FILENAME "myfile.txt"
- typedef struct
- {
- int id;
- float x_coord;
- float y_coord;
- char name[ MAX_LINE+1 ];
- }MY_TYPE_T;
- #define MAX_OBJECTS 3
- /*Initialize an array of three objects*/
- MY_TYPE_T objects[ MAX_OBJECTS ]={
- {0,1.5,8.3,"Frist-object"},
- {1,4.5,6.3,"Second-object"},
- {2,3.5,7.5,"Thrid-object"},
- };
- int main( )
- {
- int i;
- FILE* fout;
- fout=fopen(FILENAME,"w" );
- if( fout==( FILE* )NULL )
- {
- printf( "%s: %s\n",FILENAME,strerror( errno ) );
- exit( -1 );
- }
- printf( "Writing...\n" );
- for( i=0;i<MAX_OBJECTS;++i )
- {
- fprintf( fout,"%d %f %f %s\n",objects[ i ].id,objects[ i ].x_coord,
- objects[ i ].y_coord,objects[ i ].name);
- }
- fclose( fout );
- printf("Write over\n");
- printf("Read from file\n");
- FILE* fin=fopen( FILENAME,"r" );
- if( fin==(FILE*)NULL )
- {
- printf( "%s: %s\n",FILENAME,strerror( errno ) );
- exit( -1 );
- }
- MY_TYPE_T objects;
- while( !feof(fin) )
- {
- fscanf( fin,"%d %f %f %s\n",&objects.id,&objects.x_coord,
- &objects.y_coord,&objects.name);
- printf("%d %f %f %s\n",objects.id,objects.x_coord,
- objects.y_coord,objects.name);
- }
- fclose( fin );
- return 0;
- }
- /**************二进制文件的读写*****************/
- /*使用fwirte、fread进行二进制文件的读写
- 读写二进制文件的时候,要注意的重要问题是可移植性和字序问题。
- Intel采用小字节序,PowerPC和网络采用大字节序
- */
- #include <stdio.h>
- #include <errno.h>
- #define MAX_LINE 40
- #define FILENAME "myfile.bin"
- typedef struct
- {
- int id;
- float x_coord;
- float y_coord;
- char name[ MAX_LINE+1 ];
- }MY_TYPE_T;
- #define MAX_OBJECTS 3
- /*Initialize an array of three objects*/
- MY_TYPE_T objects[ MAX_OBJECTS ]={
- {0,1.5,8.3,"Frist-object"},
- {1,4.5,6.3,"Second-object"},
- {2,3.5,7.5,"Thrid-object"},
- };
- int main( )
- {
- int i;
- FILE* fout;
- fout=fopen(FILENAME,"w" );
- if( fout==( FILE* )NULL )
- {
- printf( "%s: %s\n",FILENAME,strerror( errno ) );
- exit( -1 );
- }
- printf( "Writing...\n" );
- fwrite( ( void* )objects,sizeof( MY_TYPE_T ),3,fout );
- fclose( fout );
- //使用fread/fseek/rewind读取二进制结构体数据
- printf( "Read from file\n" );
- MY_TYPE_T object;
- FILE* fin=fopen( FILENAME,"r" );
- if( fin==( FILE* )NULL )
- {
- printf( "%s: %s\n",FILENAME,strerror( errno ) );
- exit( -1 );
- }
- //定位到第三个结构体
- fseek( fin,( 2*sizeof( MY_TYPE_T ) ),SEEK_SET );
- fread( &object,sizeof( MY_TYPE_T ),1,fin );
- printf("%d %f %f %s\n",object.id,object.x_coord,
- object.y_coord,object.name);
- fseek( fin,( 1*sizeof( MY_TYPE_T ) ),SEEK_SET );
- fread( &object,sizeof( MY_TYPE_T ),1,fin );
- printf("%d %f %f %s\n",object.id,object.x_coord,
- object.y_coord,object.name);
- //把文件指针复位到文件开头
- rewind( fin );
- fread( &object,sizeof( MY_TYPE_T ),1,fin );
- printf("%d %f %f %s\n",object.id,object.x_coord,
- object.y_coord,object.name);
- fclose( fin );
- return 0;
- }
- /************************************************/
- /*
- 总结:上面所有的函数都是有一些基本API来实现的。比如:open/read/write/fdopen/pread/pwrite.
- 文件的输入输出API函数也可以用于管道和套接字。另外,文件和字符串操作是面向对象脚本语言的强项,如Ruby,Python.
- */
Linux文件编程实例的更多相关文章
- linux内核模块编程实例
linux内核模块编程实例 学号:201400814125 班级:计科141 姓名:刘建伟 1.确定本机虚拟机中的Ubuntu下Linux的版本 通过使用命令uname -a/uname -r/una ...
- linux文件编程----系统调用
linux中文件编程可以使用两种方法: 1).linux系统调用 2).C语言库函数 前者依赖于linux系统,后者与操作系统是独立的. 在 linux系统中,所有打开的文件也对应一个数字,这个数字由 ...
- Linux 文件编程、时间编程基本函数
文件编程 文件描述符 fd --->>>数字(文件的身份证,代表文件身份),通过 fd 可找到正在操作或需要打开的文件. 基本函数操作: 1)打开/创建文件 int open (co ...
- Linux多线程编程实例解析
Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...
- Linux c编程实例_例子
例一:字符与整型变量的实现 #include <stdio.h> int main() { int c1,c2; char c3; c1='a'-'A'; c2='b'-'B'; c3=; ...
- 多功能电子通讯录(涉及到了双向链表的使用,Linux文件编程等等)
readme.txt //作为一个程序员,我们咋么能不写用户手册呢!MSP的我觉得用户体验是王道,苹果手机的用户体验的确不错!不过WP加油!我去,扯远了!赶紧看我的程序吧! 歡迎使用多功能電子通訊錄 ...
- Linux网络编程实例解析
**************************************************************************************************** ...
- linux 定时器编程实例(完善中).....
最近在写linux 下的定时器编程实验,测试发现 usleep函数在 x86 架构下的定时还是比较准确的,在arm9下 就不太准了. 今天用linux 下的setitimer()函数进行了定时 器的测 ...
- Linux多进程编程实例
前言:编写多进程程序时,我们应该了解一下,创建一个子进程时,操作系统内核是怎样做的.当通过fork函数创建新的子进程时,内核将父进程的用户地址空间的内容复制给子进程,这样父子进程拥有各自独立的用户空间 ...
随机推荐
- 转:Mysql读写分离实现的三种方式
1 程序修改mysql操作类可以参考PHP实现的Mysql读写分离,阿权开始的本项目,以php程序解决此需求.优点:直接和数据库通信,简单快捷的读写分离和随机的方式实现的负载均衡,权限独立分配缺点:自 ...
- (转载)URL与URI的区别
(转载)http://blog.csdn.net/eagle51998/article/details/372052 1 URL(Uniform Resoure Locator:统一资源定位器)是W ...
- CodeForces 588A
题目链接: http://codeforces.com/problemset/problem/588/A 解题思路: 这个题目很简单, 就是前一天肉的价格比后面几天低还是高,如果是高的话,只要买当天份 ...
- 暴力求解——除法 Division,UVa 725
Description Write a program that finds and displays all pairs of 5-digit numbers that between them u ...
- Nodejs in Visual Studio Code 14.IISNode与IIS7.x
1.开始 部署IISNode环境请参考:Nodejs in Visual Studio Code 08.IIS 部署Nodejs程序请参考:Nodejs in Visual Studio Code 1 ...
- Sentry Server 日志记录分析服务
原文地址:http://blog.shanbay.com/archives/998 或许你不太会喜欢异常,特别是那些发生后继而沉默在应用日志里那些,你不知道从何开始,因为它们看起来并非那么平易近人,但 ...
- Ural1297 Palindrome(后缀数组)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12406 [题意] 求最长回文子串. [思路] 将字符串 ...
- linux0.12 解决编译问题常用命令
解决编译问题时,经常需要修改所有的Makefile,特别定义了下面几条命令方便修改. function msed() { find -name "Makefile" -exec s ...
- 蘑菇街 App 的组件化之路
在组件化之前,蘑菇街 App 的代码都是在一个工程里开发的,在人比较少,业务发展不是很快的时候,这样是比较合适的,能一定程度地保证开发效率. 慢慢地代码量多了起来,开发人员也多了起来,业务发展也快了起 ...
- Keywords Search - HDU 2222(AC自动机模板)
题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串. 分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...