文件I/O编程
1 基于linux系统的文件编程
创建文件
int creat(const char * filename,mode_t mode)
filename:要创建的文件名
mode:创建模式
常见模式:
S_IRUSR 可读 4
S_IWUSR 可写 2
S_IXUSR 可执行 1
S_IRWXU 可读、写、执行
示例代码如下:
[retacn@localhost tmp]$ vi file_create.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void create_file(char *filename){
/*指定创建文件的属性*/
if(creat(filename,0755)<0){
printf("create file %s failure!\n",filename);
exit(EXIT_FAILURE);
}else{
printf("create file %s success!\n",filename);
}
}
int main(int argc,char *argv[]){
int i;
if(argc<2){
perror("you haven't input the filename ,please try again!\n");
exit(EXIT_FAILURE);
}
for(i=1;i<argc;i++){
create_file(argv[i]);
}
exit(EXIT_SUCCESS);
return 0;
}
编译
[retacn@localhost tmp]$ gcc file_create.c -o file_create
运行程序创建文件
[retacn@localhost tmp]$ ./file_create hello
create file hello success!
文件描述
所有打开的文件对应一个文件描述符
打开文件
函数原型
int open(const char *pathname,int flags)
int open(const char *pathname,int flags,mode_t mode)
pathname:要打开的文件名(包含路径,缺省为当前路径)
flags:打开标志
常见的打开标志:
O_RDONLY 只读方式打开
O_WRONLY 只写
O_RDWR 读写
O_CREAT 若打开文件不存在,则自动创建(需要指定文件的访问权限mode)
O_EXCL 如果O_CREAT被设置,此指令会检查文件是否存在
O_NOCTTY 如果打开的文件为终端设备,则不会将该终端机当成进程控制终端机
O_TRUNC 若文件存在并且以可写的方式打开,此时文件长度清零,资料消失
O_APPEND 追加方式打开
O_NONBLOCK 非阻塞方式打开,无论有无数据读取或等待,立即返回进程中
示例代码如下:
[retacn@localhost tmp]$ vi file_open.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc,char *argv[]){
int fd;
if(argc<2){
puts("please input the open file pathname!\n");
exit(1);
}
if((fd=open(argv[1],O_CREAT | O_RDWR,0755))<0){
perror("open file failure!\n");
exit(1);
}else{
printf("open file %d success!\n",fd);
}
close(fd);
exit(0);
return 0;
}
[retacn@localhost tmp]$ gcc file_open.c -o file_open
[retacn@localhost tmp]$ ./file_open hello
open file 3 success!
文件的关闭
int close(int fd)
fd:文件描述符
读取文件内容
int read(int fd,const void *buf,size_t length)
fd:文件描述符
buf:读取数据放入缓冲区
length:数据长度
向文件中写入数据
int write(int fd,const void *buf,size_t length)
移动文件的读写位置
int lseek(int fd,offset_t offset,int whence)
文件的读写指针相对whence移动offset个字节
offset:如果取负值,表示向前移动
whence参数值:
SEEK_SET:相对文件开头
SEEK_CUR:相对文件读写指针的当前位置
SEEK_END:相对文件末尾
示例代码:
取得文件长度:
lseek(fd,0,SEEK_END)
文件的访问判断
int access(const char *pathname,int mode)
pathname:文件名
mode:要判断的访问权限
R_OK 可读
W_OK 可写
X_OK 可执行
F_OK 文件存在
正常返回0
示例代码如下:
/*实现文件的考贝*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#define BUFFER_SIZE 1024
int 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);
exit(0);
return0;
}
[retacn@localhost tmp]$ gcc file_cp.c -o file_cp
[retacn@localhost tmp]$ ./file_cp cp_test cp_test2
文件的描述词
int fcntl(int fd,int cmd)
int fcntl(int fd,int cmd,long arg)
int fcntl(int fd,int cmd,struct flock *lock)
fd:表示要设置的文件描述词
cmd:要操作的指令
文件的锁定
int flock(int fd,int operation)
fd:目标文件
operation:锁定、解除操作
相关推荐
深入探究文件 I/O:深入了解了文件 I/O 中的一些细节,譬如文件的管理方式、 错误返回的处理、空洞文件、O_APPEND 和 O_TRUNC 标志、原子操作与竞争冒险等等
标准I/O库-I.MX6U嵌入式Linux C应用编程学习笔记基于正点原子阿尔法开发板
GUN/LINUX系统编程学习笔记,分享快乐。
Linux系统编程是指在Linux操作系统上进行程序开发的一种形式。它涵盖了多个方面,包括文件操作、进程管理、内存管理、网络编程等。 Linux系统编程涵盖了众多领域,从基础的文件操作到高级的网络通信和多线程编程,为...
linux系统 编程 开发 笔记 如果 大家 需要 可以 下载 看看 啊 希望 对你 linux 系统学习 有一定的 帮助 啊
Linux编程笔记,Linux开发必备,学习资料
Linux系统是一个免费使用和自由传播的类Unix操作系统,基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统,Linux是许多企业...
linux编程学习笔记PDF资料下载
Linux系统是一个免费使用和自由传播的类Unix操作系统,基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统,Linux是许多企业...
Linux系统是一个免费使用和自由传播的类Unix操作系统,基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统,Linux是许多企业...
Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的UNIX工具软件、应用程序和网络协议。它支持32位和64位硬件。Linux继承了Unix...
这是linux嵌入式Socket网络编程学习笔记,想要学习linux嵌入式Socket网络编程的同学可以下载学习
Linux系统编程笔记,是学习Linux系统的初学者的入门好书。
非常详细的Linux_shell编程学习笔记,能帮助到初学者增加理解。
学习笔记:LINUX与UNIX_Shell编程指南
第一个函数表示将 a.b.c.d 的 IP转换为 32 位的 I P,存储在 inp 指针里面.第二个是将 32 位 IP转换为 a.b.c.d 的格式 服务信息函数 在网络程序里面我们有时候需要知道端口.IP和服务信息.这个时候我们可以使用以下...
我的linux编程学习笔记(一).pdf
一、初识 Linux shell Linux 系统可划分为以下 4 部分。 Linux 内核 GNU 工具 图形化桌面环境 应用软件 内核主要负责以下 4 种功能。 系统内存管理 内核通过硬盘上称为交换空间(swap space)的存储区域来实现虚拟...
我的linux编程学习笔记(一)分享.pdf