• Linux系统编程_文件编程第2天:写整数、结构体,fopen等


    1. 文件编程小应用之修改程序的配置文件(407.10)

    在这里插入图片描述

    • FILE/demo14.c
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char **argv){
    	int fdSrc;
    
    	char *readBuf=NULL;
    
    	if(argc != 2){
    		printf("pararm error\n");
    		exit(-1);
    	}
    
    	fdSrc = open(argv[1],O_RDWR);//打开要修改的源配置文件
    	
    	int size = lseek(fdSrc,0,SEEK_END);
    	readBuf=(char *)malloc(sizeof(char)*size + 8);//给readBuf开辟空间
    	
    	lseek(fdSrc,0,SEEK_SET);
    	int n_read = read(fdSrc,readBuf,size);//读源文件
    	
    	char *p = strstr(readBuf,"LENG=");//找到readBuf中“LENG=”的首地址(即L)
    	if(p==NULL){
    		printf("not found\n");
    		exit(-1);
    	}
    	
    	p = p+strlen("LENG=");//偏移至“LENG=”之后的一位
    	*p = '5';//修改源文件需要更改的字符为'5'	
    	
    	lseek(fdSrc,0,SEEK_SET);//定位至源文件头部
    	int n_write = write(fdSrc,readBuf,strlen(readBuf));//写入修改后的readBuf
    	
    	close(fdSrc);//关闭源文件
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    在这里插入图片描述

    2. 写一个整数到文件(408.11)

    • FILE/demo15.c(不能直接写数字5,//应该是’5’)
    *p = 5;//不能直接写数字5,应该是'5'
    
    • 1
    • FILE/demo16.c(写入一个整数)
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(){
    	int fd;
    	
    	int data = 100;
    	int data2;
    
    	fd = open("./file1",O_RDWR);
    
    	int n_write = write(fd,&data,sizeof(int));//把100写到fd
    
    	lseek(fd,0,SEEK_SET);
    
    	int n_read = read(fd, &data2, sizeof(int));//从fd读数据放到data2
    	
    	printf("read %d \n",data2);
    	close(fd);
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    在这里插入图片描述

    • FILE/demo17.c(写入一个结构体)
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    struct Test{
    	int a;
    	char c;
    };
    
    int main(){
    	int fd;
    	
    	struct Test data = {100,'a'};
    	struct Test data2;
    
    	fd = open("./file1",O_RDWR|O_TRUNK);//打开
    
    	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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在这里插入图片描述

    3. 写结构体数组到文件(409.12)

    • FILE/demo18.c(写入多个结构体)
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    struct Test
    {
    	int a;
    	char c;
    
    };
    
    int main()
    {
    	int fd;
    	
    	struct Test data[2] = {{100,'a'},{101,'b'}};
    	struct Test data2[2];
    
    	fd = open("./file1",O_RDWR);
    	int n_write = write(fd,&data,sizeof(struct Test)*2);//数组可以直接读写,因其地址空间是连续的,可以读写一个块
    							    //链表不可,应当遍历链表,一次读一个结构体						
    	lseek(fd,0,SEEK_SET);
    
    	int n_read = read(fd, &data2, sizeof(struct Test)*2);
    	
    	printf("read %d,%c \n",data2[0].a,data2[0].c);
    	printf("read %d,%c \n",data2[1].a,data2[1].c);
    	close(fd);
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    在这里插入图片描述

    4. 标准C库对文件操作引入(410.13)

    5. 标准C库打开创建文件读写文件光标移动(411.14)

    #include 
    #include 
    
    int main(){
    	FILE *fp;
    	char *str = "cui jia qi.";
    	char readBuf[128] = {0};
    	//FILE *fopen(const char *path, const char *mode);
    	fp = fopen("./cui.txt","w+");//"w+":如果没有则创建这个文件(类似open的O_CREAT)
    
    	//size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
    	//ptr  buf 缓冲区
    	//size  sizeof char  一次写一个char
    	// geshu 写多少次char
    	// which file 写到哪里
    	fwrite(str,sizeof(char),strlen(str),fp);
    //	fwrite(str,sizeof(char)*strlen(str),1,fp);//一次写完所有char
    
    	fseek(fp,0,SEEK_SET);//光标定位至头(的0个后面)
    	
    //	size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
    	fread(readBuf,sizeof(char),strlen(str),fp);
    	//fread(readBuf,sizeof(char)*strlen(str),1,fp);
    	
    	printf("read data: %s\n",readBuf);
    	fclose(fp);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    6. 标准C库写入结构体到文件(412.15)

    • FILE/demo21.c(观察nwrite的值和第三个参数有关,而nread取决于真实字节大小)(nwrite nread 和 write read 返回的一样,都是返回有多少个字节)
    #include 
    #include 
    
    int main()
    {
    	//FILE *fopen(const char *path, const char *mode);
    
    	FILE *fp;
    	char *str = "chenlichen hen shuai";
    	char readBuf[128] = {0};
    
    	fp = fopen("./chen.txt","w+");
    
    	//size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
    	//ptr  buf
    	//size  sizeof char   
    	// geshu 
    	// which file
    	int nwrite = fwrite(str,sizeof(char),100,fp);//nwrite的值取决于第三个参数的值
    //	fwrite(str,sizeof(char)*strlen(str),1,fp);
    	fseek(fp,0,SEEK_SET);
    //	size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
    	//int nread = fread(readBuf,sizeof(char),strlen(str),fp);
    	int nread = fread(readBuf,sizeof(char)*strlen(str),100,fp);//nread的值跟第三个参数无关,为实际有多少字节
    	
    	printf("read data: %s\n",readBuf);
    	printf("read=%d,write = %d\n",nread,nwrite);
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • FILE/demo23.c(写入结构体)
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    struct Test{
    	int a;
    	char c;
    };
    
    int main(){
    	FILE *fp;
    	
    	struct Test data = {100,'a'};
    	struct Test data2;
    
    	fp = fopen("./file1","w+");
    
    	int n_write = fwrite(&data,sizeof(struct Test),1,fp);//1次写整个结构体的大小
    
    	fseek(fp,0,SEEK_SET);
    
    	int n_read = fread(&data2,sizeof(struct Test),1,fp);
    	
    	printf("read %d,%c\n",data2.a,data2.c);
    	
    	fclose(fp);
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    7. 文件其它函数讲解及文件收尾(413.16)

    #include 
    #include 
    
    int main(){
    	FILE *fp;
    	int i;
    	char *str = "chenlichen hen shuai o!";
    	int len = strlen(str);
    
    	fp = fopen("./test.txt","w+");
    	for(i=0;i<len;i++){		
    
    		fputc(*str,fp);      //把str写入fp
    		str++;
    	}
    	
    	fclose(fp);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • FILE/demo25.c
    #include 
    #include 
    
    int main(){
    	FILE *fp;
    	int i;
    	char c;
    
    	fp = fopen("./test.txt","r");
    
    	while(!feof(fp)){// nonezero if reach end of file//到达文件尾巴时为非0,未到达尾巴时为0
    		
    		c = fgetc(fp);//把fp里面的字节一个个取出
    		printf("%c",c);
    	}
    	fclose(fp);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    远程控制 Linux 系统的软件下载
    C++学习day3
    阿里巴巴中国站按图搜索1688商品(拍立淘) API 返回值说明
    四、RabbitMQ TTL、死信队列以及延迟队列
    你学不好英语,可能是精神内耗太多了
    拖延症:关于如何停止拖延的科学指南
    Java:Session 会话详解
    SQL Server 安装后,服务器再改名,造成名称不一致,查询并修改数据库服务器真实名称
    智能通风柜手势控制界面设计与实现
    Intellij IDEA快捷键大全汇总(二)
  • 原文地址:https://blog.csdn.net/Jaci133/article/details/133817422