不带缓存的IO操作又称底层IO操作。文件底层I/O操作的系统调用主要用到5个函数:open()、close()、read()、write()、lseek()。这些函数的特点是不带缓存,直接对文件进行操作。 虽然不带缓存的文件IO操作程序不能移植到非POSIX标准的系统(如Windows系统)上去,但是在嵌入式程序设计、TCP/IP的Socekt套接字程序设计、多路IO操作程序设计等方面应用广泛。因此,不带缓存的文件IO程序设计是Linux文件操作程序设计的重点。

01.jpg (14.33 KB, 下载次数: 0)
下载附件 保存到相册 设为封面
7 天前 上传
02.jpg (99.66 KB, 下载次数: 0)
下载附件 保存到相册 设为封面
7 天前 上传
03.jpg (24.61 KB, 下载次数: 0)
下载附件 保存到相册 设为封面
7 天前 上传
04.jpg (32.76 KB, 下载次数: 0)
下载附件 保存到相册 设为封面
7 天前 上传
05.jpg (32.46 KB, 下载次数: 0)
下载附件 保存到相册 设为封面
7 天前 上传
06.jpg (46.26 KB, 下载次数: 0)
下载附件 保存到相册 设为封面
7 天前 上传
3. 使用实例 程序功能:设计一个程序,要求从一个源文件src_file(如不存在则创建)中读取倒数第二个10KB数据并复制到目标文件dest_file。 程序说明: 程序代码: #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> #define BUFFER_SIZE 1024 #define SRC_FILE_NAME "src_file" #define DEST_FILE_NAME "dest_file" #define OFFSET 20480 int main() { int src_file, dest_file; unsigned char buff[BUFFER_SIZE]; int real_read_len; int flag=10; src_file = open(SRC_FILE_NAME, O_RDONLY); dest_file = open(DEST_FILE_NAME, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); if (src_file < 0 || dest_file < 0) { printf("Open file error\n"); exit(1); } lseek(src_file, -OFFSET, SEEK_END); while ((real_read_len = read(src_file, buff, sizeof(buff))) > 0||flag>0) { flag--; write(dest_file, buff, real_read_len); } close(dest_file); close(src_file); return 0; } 结果: # ./copy_file # ls –lh dest_file 本文转载于唯C教育,【Linux基础】不带缓存的IO操作
http://www.weicedu.com/forum.php?mod=viewthread&tid=100&fromuid=4
(出处: http://www.weicedu.com/)