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

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

在这里插入图片描述

  • FILE/demo14.c
#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("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;
}

在这里插入图片描述

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

  • FILE/demo15.c(不能直接写数字5,//应该是’5’)
*p = 5;//不能直接写数字5,应该是'5'
  • FILE/demo16.c(写入一个整数)
#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 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;
}

在这里插入图片描述

  • FILE/demo17.c(写入一个结构体)
#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|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;
}

在这里插入图片描述

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

  • FILE/demo18.c(写入多个结构体)
#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[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;
}

在这里插入图片描述

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

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

#include <stdio.h>
#include <string.h>

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;
}

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

  • FILE/demo21.c(观察nwrite的值和第三个参数有关,而nread取决于真实字节大小)(nwrite nread 和 write read 返回的一样,都是返回有多少个字节)
#include <stdio.h>
#include <string.h>

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;
}
  • FILE/demo23.c(写入结构体)
#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(){
	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;
}

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

#include <stdio.h>
#include <string.h>

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;
}
  • FILE/demo25.c
#include <stdio.h>
#include <string.h>

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值