文件的读写1
来源:互联网 发布:英雄无敌 mac 10.10 编辑:程序博客网 时间:2024/06/07 02:42
直接上代码吧,文件的读写,今天没有多少时间来解释;只是觉得工作的时候,时间好紧,貌似很难做到一天一篇文章,就连最简单的刷题都不行,忧伤;我需要多搞一些时间出来做这个事情吧。
#include <unistd.h>#include <fcntl.h>#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <errno.h>#include <string.h>#define BUFFER_SIZE 1024int main(int argc, char **argv){ int from_fd, to_fd; int bytes_read, bytes_write; char buffer[BUFFER_SIZE]; char *ptr; if (argc != 3) { fprintf(stderr, "Usage: %s fromfile tofile \n\a", argv[0]); exit(-1); } if ((from_fd = open(argv[1], O_RDONLY)) == -1) { fprintf(stderr, "Open %s Error: %s\n",argv[1], strerror(errno)); exit(-1); } if ((to_fd = open(argv[2], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) { fprintf(stderr, "Open %s Error: %s\n",argv[2], strerror(errno)); exit(-1); } while (bytes_read = read(from_fd, buffer, BUFFER_SIZE)) { if ((bytes_read == -1) && errno != EINTR) break; else if (bytes_read > 0) { ptr = buffer; while(bytes_write = write(to_fd, ptr, bytes_read)) { if ((bytes_write == -1 && errno != EINTR))break; else if (bytes_write == bytes_read)break; else if (bytes_write > 0) { ptr += bytes_write; bytes_read -= bytes_write; } } if (bytes_write ==-1)break; } } close(from_fd); close(to_fd); return 0;}
- 还有其他可行的方法,期待今天还有时间再发一篇吧。
0 0
- 文件的读写1
- 文件的读写1
- 文件的读写操作1
- 文件操作《1》文件的读写
- MFC 文件操作1 文件的读写
- 串口的读写,文件读写
- 【1】文件流的读写操作
- INI文件的读写
- 读写文件的例子
- INI文件的读写
- 关于文件的读写
- 文件的读写操作
- 文件的读写操作
- properties文件的读写
- 读写服务器端的文件
- 位图文件的读写
- C++文件的读写
- ini文件的读写
- 六步实现Rest风格的API
- iOS的Socket开发基础
- 动画片1
- K-近邻法(KNN)的实现
- 缓冲区溢出十年来攻击和防卫的弱点
- 文件的读写1
- 解决ubuntu14.04下Qt 5.3.1下的QtCreator fcitx,ibus不能输入中文
- Leetcode 之 Candy
- 本地sqlyog连接错误
- JAVA基础----多线程
- c++顺序容器之array
- Counting Binary Trees - HDU 3240 卡特兰数+逆元
- HostConfig.deployDirectories
- opencv 使用鼠标选定矩形框