Linux学习------文件编程

文章目录

一、什么是文件编程?

  1. 在windows中,我们如何编写一个文件呢?
    步骤:打开/新建文档---->编辑文档---->保存文档---->关闭文档。
  2. 那在Linux中如何编写一个文件呢?
    步骤:打开(open)---->读写(read/write)---->关闭(close)
  3. 文件编程目的
    关心如何用代码操作文件。实现文件创建,打开,编辑等自动化执行

二、常用文件函数

  1. 文件打开/创建:open()函数
  2. 创建文件:creat()函数
  3. 文件写入:write()函数
  4. 文件读取:read()函数
  5. 关闭文件:close()函数

学会以上函数的使用

三、例子

  1. 打开写入读取

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <string.h> 
    #include <stdlib.h>
    	
    int main(){
    
    	int fd;
    	char writebuf[128];
    	char *readbuf;
    	int filesize = 0;
    	int n_write, n_read;
    	
    	// 记得要用if判断 打开、读取、写入是否成功
    	if((fd = open("./file1", O_RDWR)) == -1){
    		printf(" open file1 failed!\n");
    		// fd = open("./file1", O_RDWR|O_CREAT|O_TRUNC, 0600);
    		fd = open("./file1", O_RDWR|O_CREAT, 0600);
    		if(fd > 0){
    			printf("create file1 success!\n");
    		}else{
    			printf("create file1 failed!\n");
    			exit(-1);
    		}
    	}else{
    		printf("open file1 success!\n");
    	}
    	puts("input:");
    	gets(writebuf);
    	if((n_write = write(fd, writebuf, strlen(writebuf))) == -1){
    		perror("write");
    		exit(-1);
    	}
    		
    	filesize = lseek(fd, 0, SEEK_END);
    	lseek(fd, 0, SEEK_SET);
    	readbuf = (char *)malloc(sizeof(char)*filesize + 1);
    	
    	if((n_read = read(fd, readbuf, n_write)) == -1){
    		perror("read");
    		exit(-1);
    	}
    	printf("read %d from file readbuf:%s\n", n_read, readbuf);
    	close(fd);
    	
    	return 0;
    }
    

    if判断之后适用perrorexit函数找出错误并退出程序

  2. 自定义一个cp函数 cp file1 file2
    实现方法:1.打开Src; 2.复制src内容到buf缓冲区; 3.打开Des; 4.从buf复制到Des; 5.关闭文件

    #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, fdDes;
    	char *readbuf = NULL;
    	
    	// 参数数量判断
    	if(argc != 3){
    		puts("param error!");
    		exit(-1);
    	}
    	// argv[1] 为字符串数据, open第一个参数 目录缺省为当前文件夹
    	if((fdSrc = open(argv[1], O_RDWR)) == -1){
    		perror("fdSrc");
    		exit(-1);
    	}
    	
    	// 求算字符串长度
    	int filesize = lseek(fdSrc, 0, SEEK_END);  
    	lseek(fdSrc, 0, SEEK_SET);
    	readbuf = (char *)malloc(sizeof(char) * filesize + 1);
    	int n_read = read(fdSrc, readbuf, filesize);
    	close(fdSrc);
    
    	if((fdDes = open(argv[2], O_RDWR|O_CREAT, 0600)) == -1){
    		perror("fdDes");
    		exit(-1);
    	}
    
    	lseek(fdDes, 0, SEEK_SET); 	// 相当于O_TRUNC,写之前移动光标到开头
    	int n_write = write(fdDes, readbuf, strlen(readbuf));
    
    	close(fdDes);
    	// puts("Operate Success!!!");
    	return 0;
    }
    
  3. 修改存档中的数据,现有如下文档数据

    SPEED=3
    LENG =3   // 修改LENG = 66
    SCORE=9
    LEVEL=5
    

    实现方法:从file1读取内容到buf缓冲区,用strstr函数查找字符串LENG,修改内容,写入file1
    代码:

    #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){
    		puts("param error!");
    		exit(-1);
    	}
    	// 打开file1
    	if((fdSrc = open(argv[1], O_RDWR)) == -1){
    		perror("fdSrc");
    		exit(-1);
    	}
    	// 读取数据到buf
    	int filesize = lseek(fdSrc, 0, SEEK_END);
    	lseek(fdSrc, 0, SEEK_SET);
    	readbuf = (char *)malloc(sizeof(char) * filesize + 1);
    	int n_read = read(fdSrc, readbuf, filesize);
    	
    	// 查找LENG内容,用指针p定位到要求改内容的位置,并判断是否存在该内容
    	char *p = strstr(readbuf, "LENG =");
    	if(p == NULL){
    		printf("NO exist the string!!\n");
    	}
    	p = p+strlen("LENG =");
    
    	// 修改为自定义内容
    	puts("inputs datas!");
    	gets(p);
    	
    	// 写回到file1
    	lseek(fdSrc, 0, SEEK_SET); // O_TRUNC
    	int n_write = write(fdSrc, readbuf, strlen(readbuf));
    	
    	return 0;
    }
    

    关于strstr(haystack, needle)函数:在字符串 haystack 中查找第一次出现字符串 needle 的位置,返回值为指针

    参考:C 语言库函数 - strstr()

  4. 整数、结构体、结构体数组到文件

    #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 data = {100,'a'};
    	struct Test data2;
    
    	fd = open("./file1",O_RDWR);
    	// 传入的参数为结构体的指针,其他类型参数等同
    	int n_write = write(fd,&data,sizeof(struct Test));		
    	lseek(fd,0,SEEK_SET);
    	int n_read = read(fd, &data2, sizeof(struct Test));		
    	printf("read %d,%c \n",data2.a,data2.c);
    	close(fd);
    
    	return 0;
    }
    

四、标准C语言库的文件操作(open与fopen的区别)

面试常问: 总结open与fopen的区别

在这里插入图片描述

注意fgetc使用的时候会存在内部指针,每次使用都会向后偏移一个字节

如何使用:标准C库打开创建文件读写文件以及光标移动?

五、面试常问sizeof与strlen的区别

  1. sizeof是操作符,而strlen是库函数。
  2. sizeof的参数可以是数据的类型,也可以是变量,而strlen参数只能是结尾为’\0’的字符串
  3. sizeof在编译器编译时就计算出结果,而strlen必须在运行时才能计算出来。
  4. sizeof计算数据类型占内存的大小(计算指针的长度),strlen计算字符串实际长度。
  5. sizeof的参数为数组不退化, 传递给strlen时数组会被退化为指针;

使用小技巧:遇见要分配内存的用sizeof,遇见要输出字符串长度的用strlen。

  • 举例:

    分配内存:

    readBuf = (char *)malloc(sizeof(char) * len + 1); 
    // 预留给字符串结尾'\0'
    

    计算字符串长度:

    int n_write = write(fdDes, readBuf, strlen(readBuf));
    filesize = lseek(fd, 0, SEEK_END);
    

    tips:
    gets puts strlen strstr 等函数参数均为指针或者字符串

参考链接:

Linux系统编程——文件编程:https://blog.csdn.net/qq_52902991/article/details/131261337
Linux系统编程之文件编程(一)
https://blog.csdn.net/weixin_46899589/article/details/130716525

  • 47
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值