Linux系统编程(1)——文件编程

一、理论知识

  1. 文件描述符
    在这里插入图片描述
  2. 文件编程的一般步骤
    在这里插入图片描述
    在这里插入图片描述
  3. Linux文件管理简述
    在这里插入图片描述

二、open打开文件函数

  1. 参数说明
    在这里插入图片描述

    Pathname:要打开的文件名(含路径,缺省为当前路径)
    Flags:
        O_RDONLY 只读打开         O_WRONLY 只写打开         O_RDWR  可读可写打开
    
        当我们附带了权限后,打开的文件就只能按照这种权限来操作。
        以上这三个常数中应当只指定一 个。下列常数是可选择的:     
            O_CREAT 若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限。
            O_EXCL 如果同时指定了OCREAT,而文件已经存在,则出错。        
            O_APPEND 每次写时都加到文件的尾端。
            O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。
    
    Mode:一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限
    
  2. 源码

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    
    int main(void)
    {
    	int fd;
    
    	fd = open("./file1", O_RDWR);
    	if(fd == -1) {
    		printf("creat fail\n");
    		fd = open("./file1", O_RDWR|O_CREAT, 0600);
    		printf("fd = %d\n", fd);
    		if(fd > 0) {
    			printf("creat file1 success\n");
    		}
    	}
    
    	return 0;
    }
    

三、write写入文件

  1. 参数说明
    在这里插入图片描述
  2. 源码
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    
    int main(void)
    {
    	int fd;
    	char *buf = "jiangyo handsome!";
    
    	fd = open("./file1", O_RDWR);
    	if(fd == -1) {
    		printf("creat fail\n");
    		fd = open("./file1", O_RDWR|O_CREAT, 0600);
    		printf("fd = %d\n", fd);
    		if(fd > 0) {
    			printf("creat file1 success\n");
    			write(fd, buf, strlen(buf));
    			close(fd);
    		}
    	}
    
    	return 0;
    }
    

四、read读取文件

  1. 参数说明
    在这里插入图片描述
  2. 源码
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	int fd;
    	int n_write;
    	char *buf = "jiangyo handsome!";
    
    	fd = open("./file1", O_RDWR);
    	if(fd == -1) {
    		printf("creat fail\n");
    		fd = open("./file1", O_RDWR|O_CREAT, 0600);
    		printf("fd = %d\n", fd);
    		if(fd > 0) {
    			printf("creat file1 success\n");
    			n_write = write(fd, buf, strlen(buf));
    			close(fd);
    		}
    	}
    
    	printf("write %d Byte\n",n_write);
    
    	fd = open("./file1", O_RDWR);
    
    	char *read_buf;
    	read_buf = (char *)malloc(sizeof(char)*n_write + 1);
    	int n_read = read(fd, read_buf, n_write);
    	printf("read %d byte:%s\n", n_read, read_buf);
    	free(read_buf);
    	return 0;
    }
    

五、lseek文件光标位置

  1. 参数说明在这里插入图片描述
  2. 源码
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	int fd;
    	int n_write;
    	char *buf = "jiangyo handsome!";
    
    	fd = open("./file1", O_RDWR);
    	if(fd == -1) {
    		printf("creat fail\n");
    		fd = open("./file1", O_RDWR|O_CREAT, 0600);
    		printf("fd = %d\n", fd);
    		if(fd > 0) {
    			printf("creat file1 success\n");
    			n_write = write(fd, buf, strlen(buf));
    		}
    	}
    
    	printf("write %d Byte\n",n_write);
    
    	//lseek(fd, 0, SEEK_SET);
    	//lseek(fd, -n_write, SEEK_CUR);
    	lseek(fd, -n_write, SEEK_END);
    	char *read_buf;
    	read_buf = (char *)malloc(sizeof(char)*n_write + 1);
    	int n_read = read(fd, read_buf, n_write);
    	printf("read %d byte:%s\n", n_read, read_buf);
    	free(read_buf);
    	return 0;
    }
    
  3. 读取文件字节数
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	int fd;
    	int n_write;
    	char *buf = "jiangyo handsome!";
    
    	fd = open("./file1", O_RDWR);
    	if(fd == -1) {
    		printf("creat fail\n");
    		fd = open("./file1", O_RDWR|O_CREAT, 0600);
    		printf("fd = %d\n", fd);
    		if(fd > 0) {
    			printf("creat file1 success\n");
    			n_write = write(fd, buf, strlen(buf));
    		}
    	}
    
    	printf("write %d Byte\n",n_write);
    
    	int len = lseek(fd, 0, SEEK_END);
    	
    	printf("buf size = %d byte\n", len);
    	close(fd);
    	return 0;
    }
    

六、创建文件creat函数

  1. 参数说明
    在这里插入图片描述
  2. S_IRWXU源码
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	int fd;
    	int n_write;
    	char *buf = "jiangyo handsome!";
    
    	fd = open("./file1", O_RDWR);
    	if(fd == -1) {
    		printf("creat fail\n");
    		fd = creat("./file1", S_IRWXU);
    
    		fd = open("./file1", O_RDWR|O_TRUNC);
    		printf("fd = %d\n", fd);
    		if(fd > 0) {
    			printf("creat file1 success\n");
    			n_write = write(fd, buf, strlen(buf));
    		}
    	}
    	else {
    		fd = open("./file1", O_RDWR|O_TRUNC);	
    		printf("fd = %d\n", fd);
    		if(fd > 0) {
    			printf("open file1 success\n");
    			n_write = write(fd, buf, strlen(buf));
    		}
    	}
    
    	printf("write %d Byte\n",n_write);
    
    	int len = lseek(fd, 0, SEEK_END);
    	
    	printf("buf size = %d byte\n", len);
    	close(fd);
    	return 0;
    }
    

七、实现文件拷贝

  1. 源程序
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
    	int fdSrc, fdDec;
    	char *Readbuf=NULL;
    
    	if(argc != 3) {
    		printf("param error\n");
    		exit(-1);
    	}
    
    	fdSrc = open(argv[1], O_RDWR);
    	int size = lseek(fdSrc, 0, SEEK_END);
    	lseek(fdSrc, 0, SEEK_SET);
    	Readbuf = (char *)malloc((sizeof(char)) * size + 8);
    
    	read(fdSrc, Readbuf, size);
    
    	fdDec = open(argv[2], O_RDWR|O_CREAT|O_TRUNC, 0600);
    	write(fdDec, Readbuf, size);
    
    	close(fdSrc);
    	close(fdDec);
    
    	free(Readbuf);
    
    	return 0;
    }
    

八、配置文件的修改

  1. 源程序
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
    	int fdSrc;
    	char *Readbuf=NULL;
    
    	if(argc != 2) {
    		printf("param error\n");
    		exit(-1);
    	}
    
    	fdSrc = open(argv[1], O_RDWR);
    	int size = lseek(fdSrc, 0, SEEK_END);
    	lseek(fdSrc, 0, SEEK_SET);
    	Readbuf = (char *)malloc((sizeof(char)) * size + 8);
    
    	read(fdSrc, Readbuf, size);
    
    	char *str = strstr(Readbuf, "LENG=");
    	str = str+strlen("LENG=");
    	*str = '5';
    
    	lseek(fdSrc, 0, SEEK_SET);
    
    	write(fdSrc, Readbuf, size);
    
    	close(fdSrc);
    
    	free(Readbuf);
    
    	return 0;
    }
    

九、整型数写入文件

  1. 源程序
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	int fd;
    	int indata = 100;
    	int outdata;
    	fd = open("./file1", O_RDWR|O_CREAT, 0600);
    	write(fd, &indata, sizeof(int));
    
    	lseek(fd, 0, SEEK_SET);
    
    	read(fd, &outdata, sizeof(int));
    	printf("outdata = %d\n", outdata);
    	close(fd);
    
    	return 0;
    }
    

十、结构体写入文件

  1. 源程序
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct test
    {
    	int a;
    	char c;
    };
    
    int main()
    {
    	int fd;
    	struct test t1[2] = {{100, 'a'},{200, 'b'}};
    	struct test t2[2];
    	fd = open("./file1", O_RDWR|O_CREAT|O_TRUNC, 0600);
    	write(fd, t1, sizeof(struct test)*2);
    
    	lseek(fd, 0, SEEK_SET);
    
    	read(fd, t2, sizeof(struct test)*2);
    	printf("t0.a = %d, t0.c = %c, t1.a = %d, t1.c = %c\n", t2[0].a, t2[0].c, t2[1].a, t2[1].c);
    	close(fd);
    
    	return 0;
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值