Linux下文件读写示例
2015-08-26 12:06
633 查看
1. 对于fread或fwrite查看man手册,其中要注意返回值。
2. 上述两个函数第二个参数如果是1,那么返回值即实际传输长度,如果返回值是0,一般表示文件传完;如果返回值等于需要传输长度,那么正常;如果返回值小于指定传输长度,那么有可能是最后一次传输,因为到达文件尾,也有可能是传输错误;
如果第二个参数不是1,那么就不能通过返回值判断是否传输完,要通过对应库函数来判断是否到达文件尾。如果返回小于传输长度,有可能是正常、有可能发生错误,也要通过库函数检测。
3. 示例源码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
Function : read data from the 2page.pdf,And write the date to write.pdf
*/
char * readFileName = "./2page.pdf";
char * writeFileName = "./write.pdf";
#define BUFFER_SIZE 2048
void main()
{
int readLengh = 0;
int realReadLengh =0;
int realWriteLengh =0;
char buf[BUFFER_SIZE];
memset(buf, 0, sizeof(buf));
FILE *readFp = fopen(readFileName, "r");
if(NULL == readFp) //if failed,fread and fwrite can't go on
{
printf("readFile open failed,exit!\n");
exit(1);
}
FILE *writeFp = fopen(writeFileName, "w");
if(NULL == writeFp) //if failed,fread and fwrite can't go on
{
printf("writeFile open failed,exit!\n");
exit(1);
}
/*
fread or fwrite does not distinguish between end-of-file and error, and callers must use feof(3)
and ferror(3) to determine which occurred.
*/
readLengh = 1024;
while(1)
{
realReadLengh = fread(buf,1,readLengh, readFp);//return value equals the number of bytes transferred only when the second parameter is 1.
buf[realReadLengh] = '\0';
printf("\nrealReadLengh=%d\nbuf=\n %s\n",realReadLengh,buf);
if( realReadLengh >0 )
{
realWriteLengh = fwrite(buf,1,realReadLengh,writeFp);
printf("\nrealWriteLengh=%d\nbuf=\n %s\n",realWriteLengh,buf);
memset(buf, 0, sizeof(buf));
continue;
}
else
{
printf("read end\n");
exit(1);
}
}
}
2. 上述两个函数第二个参数如果是1,那么返回值即实际传输长度,如果返回值是0,一般表示文件传完;如果返回值等于需要传输长度,那么正常;如果返回值小于指定传输长度,那么有可能是最后一次传输,因为到达文件尾,也有可能是传输错误;
如果第二个参数不是1,那么就不能通过返回值判断是否传输完,要通过对应库函数来判断是否到达文件尾。如果返回小于传输长度,有可能是正常、有可能发生错误,也要通过库函数检测。
3. 示例源码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
Function : read data from the 2page.pdf,And write the date to write.pdf
*/
char * readFileName = "./2page.pdf";
char * writeFileName = "./write.pdf";
#define BUFFER_SIZE 2048
void main()
{
int readLengh = 0;
int realReadLengh =0;
int realWriteLengh =0;
char buf[BUFFER_SIZE];
memset(buf, 0, sizeof(buf));
FILE *readFp = fopen(readFileName, "r");
if(NULL == readFp) //if failed,fread and fwrite can't go on
{
printf("readFile open failed,exit!\n");
exit(1);
}
FILE *writeFp = fopen(writeFileName, "w");
if(NULL == writeFp) //if failed,fread and fwrite can't go on
{
printf("writeFile open failed,exit!\n");
exit(1);
}
/*
fread or fwrite does not distinguish between end-of-file and error, and callers must use feof(3)
and ferror(3) to determine which occurred.
*/
readLengh = 1024;
while(1)
{
realReadLengh = fread(buf,1,readLengh, readFp);//return value equals the number of bytes transferred only when the second parameter is 1.
buf[realReadLengh] = '\0';
printf("\nrealReadLengh=%d\nbuf=\n %s\n",realReadLengh,buf);
if( realReadLengh >0 )
{
realWriteLengh = fwrite(buf,1,realReadLengh,writeFp);
printf("\nrealWriteLengh=%d\nbuf=\n %s\n",realWriteLengh,buf);
memset(buf, 0, sizeof(buf));
continue;
}
else
{
printf("read end\n");
exit(1);
}
}
}
相关文章推荐
- L1:linux用户,用户组详细管理命令使用
- linux多线程编程入门
- linux 开启独立iptables日志
- CI在CentOS中的部署与实践LNMP
- Centos 6.6 final 安装 xtrabackup 2.2.12 小记
- linux常用命令
- linux下android开发真机设备识别不了怎么办
- 介绍几个不错的linux下载站和论坛
- linux 学习文章
- Debian Linux系统如何进入单用户模式恢复密码
- centos服务器操作要点
- linux挂载新硬盘,开机自动挂载
- Linux单独安装svn服务器-subversion
- Linux下软件的安装与卸载方法
- Linux: 介绍make menuconfig中的每个选项含义【转】
- yum搭建lnmp环境(CentOS6.6)
- linux下用alternatives进行java版本的转换
- 在CentOs7上配置Mesos+Zookeeper
- (13)Linux 的启动流程
- linux top 日志输出