Linux文件编程应用(Linux文件API和C标准文件API)

1、将字符写入文件

代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
	int fd;
	char *buf = "xiefangjing handsome!";
        fd = open("./file1",O_RDWR);
	
	if(fd<0){
                printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd>0){
                        printf("create file1 succeed\n");
                }
        }
        printf("open succeed : fd = %d\n",fd);
	

//ssize_t write(int fd, const void *buf, size_t count);
	printf("open succeed : fd = %d\n",fd);
	write(fd,buf,strlen(buf));
	
	close(fd);
		

	return 0;
}

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()
{
	int fd;
	char *buf = "xiefangjing handsome!";

	fd=open("./file1",O_RDWR);
	
	if(fd<0){
		printf("open file1 failed\n");
		fd = open("./file1",O_RDWR|O_CREAT,0600);
		if(fd>0){
			printf("create file1 succeed\n");
		}
	}
	printf("open succeed : fd = %d\n",fd);

//ssize_t write(int fd, const void *buf, size_t count);
	int n_write = write(fd,buf,strlen(buf));
	if(n_write != -1){
		printf("write %d byte to file1\n",n_write);
	}



	char *readBuf;
	readBuf=(char *)malloc(sizeof(char)*n_write+1);
	
//ssize_t read(int fd, void *buf, size_t count);
	
//off_t lseek(int fd, off_t offset, int whence);
	
//	lseek(fd,0,SEEK_SET);
//	lseek(fd,-21,SEEK_CUR);
	lseek(fd,-21,SEEK_END);
	int n_read = read(fd,readBuf,n_write);
	
	printf("read:%d,context:%s\n",n_read,readBuf);
	close(fd);
		

	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()
{
	int fd;

	fd=open("./file1",O_RDWR);
	
	int fileSize = lseek(fd,0,SEEK_END);

	printf("the file size is :%d byte\n",fileSize);
	
	close(fd);
		

	return 0;
}

4、标准输入输出(键盘录入,屏幕输出)

代码:
#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()
{
	char readBuf[128]={0};

	
	read(0,readBuf,5);

	write(1,readBuf,strlen(readBuf));
	
	printf("\ndone!\n");

	return 0;
}

5、LinuxAPI实现cp指令

代码:
#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 **argcv)
{
	int fdSrc;
	int fdDes;
	char *readBuf = NULL;
	
	if(argc != 3){
		printf("param error!\n");
		exit(-1);
	}	


	fdSrc = open(argcv[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);

	
	fdDes = open(argcv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
	write(fdDes,readBuf,strlen(readBuf));

	close(fdSrc);
	close(fdDes);
	
		

	return 0;
}

6、c语言标准文件API实现cp指令

代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc,char *argcv[])
{
	FILE *fpSrc = NULL;
	FILE *fpDes = NULL;
	char *readBuf = NULL;
	
	if(argc != 3){
		printf("param error!\n");
		exit(-1);
	}	


	fpSrc = fopen(argcv[1],"r+");

	fseek(fpSrc,0,SEEK_END);
	long int size = ftell(fpSrc);
	
	//fseek(fpSrc,0,SEEK_SET);
	rewind(fpSrc);
	
	readBuf = (char *)malloc(sizeof(char)*size+8);	

	fread(readBuf,sizeof(char),size,fpSrc);

	
	fpDes = fopen(argcv[2],"w+");
	fwrite(readBuf,sizeof(char),size,fpDes);

	fclose(fpSrc);
	fclose(fpDes);
	
		

	return 0;
}

7、C语言标准文件fgetc和fputc实现cp指令

#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argcv[])
{
	FILE *fpSrc = NULL;
	FILE *fpDes = NULL;
	
	char c;

	if(argc != 3){
                printf("param error!\n");
                exit(-1);
        }

	fpSrc = fopen(argcv[1],"r");
	fpDes = fopen(argcv[2],"w");

	while((c = fgetc(fpSrc))!=EOF ){

		fputc(c,fpDes);
		printf("%c",c);
	}

	fclose(fpSrc);
	fclose(fpDes);
	

	return 0;
}

8、修改配置文件

代码:

#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 **argcv)
{
	int fdSrc;
	char *readBuf = NULL;
	
	if(argc != 2){
		printf("param error!\n");
		exit(-1);
	}	


	fdSrc = open(argcv[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 *p = strstr(readBuf,"LENG=");
	if(p == NULL){
		printf("not found!");
		exit(-1);
	}

	p = p+strlen("LENG=");

	*p = '5';

	lseek(fdSrc,0,SEEK_SET);
	write(fdSrc,readBuf,strlen(readBuf));

	close(fdSrc);
	
		

	return 0;
}

9、向文件中写入和读取数字

代码:
#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;

	fd=open("./file1",O_RDWR|O_TRUNC);

	int data1 = 100;
	int data2 = 0;
	
	if(fd<0){
		printf("open file1 failed\n");
		fd = open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);
		if(fd>0){
			printf("create file1 succeed\n");
		}
	}
	printf("open succeed : fd = %d\n",fd);

//ssize_t write(int fd, const void *buf, size_t count);
	int n_write = write(fd,&data1,sizeof(int));
	if(n_write != -1){
		printf("write %d byte to file1\n",n_write);
	}


	lseek(fd,0,SEEK_SET);
	
//ssize_t read(int fd, void *buf, size_t count);
	
	int n_read = read(fd,&data2,n_write);
	
	printf("read:%d\n",data2);
	close(fd);
		

	return 0;
}

10、向文件中写入结构体

代码:
#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 data;
	char cdata;

};

int main()
{
	int fd;

	fd=open("./file1",O_RDWR|O_TRUNC);

	struct Test data1[2] = {{100,'A'},{200,'B'}};
	struct Test data2[2];
	
	if(fd<0){
		printf("open file1 failed\n");
		fd = open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);
		if(fd>0){
			printf("create file1 succeed\n");
		}
	}
	printf("open succeed : fd = %d\n",fd);

//ssize_t write(int fd, const void *buf, size_t count);
	int n_write = write(fd,&data1,sizeof(struct Test)*2);
	if(n_write != -1){
		printf("write %d byte to file1\n",n_write);
	}


	lseek(fd,0,SEEK_SET);
	
//ssize_t read(int fd, void *buf, size_t count);
	
	int n_read = read(fd,&data2,n_write);
	
	printf("read:%d %c\n",data2[0].data,data2[0].cdata);
	printf("read:%d %c\n",data2[1].data,data2[1].cdata);
	close(fd);
		

	return 0;
}

11、c语言标准文件API写入和读取字符

代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
	//FILE *fopen(const char *path, const char *mode);
	FILE *fp = NULL;

	fp = fopen("./xfj.txt","w+");

	char *writeBuf = "xfj hansome!";
	char *readBuf = NULL;

	//size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
	//ptr: buf
	//size: the size of a char
	//nmemb: the numbers of char
	//stream: which file

	//int n_write = fwrite(writeBuf,sizeof(char)*strlen(writeBuf),1,fp);
	fwrite(writeBuf,sizeof(char),strlen(writeBuf),fp);
	

	//size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
	//ptr: buf
	//size: the size of a char
	//nmemb: the numbers of char
	//stream: which file

	
	//int fseek(FILE *stream, long offset, int whence);

	fseek(fp,0,SEEK_SET);
	
	readBuf = (char *)malloc(strlen(writeBuf)+8);
	
	fread(readBuf,sizeof(char),strlen(writeBuf),fp);
	
	printf("read context :%s\n",readBuf);

	fclose(fp);

	return 0;
}

12、c语言标准文件API写入和读取结构体

代码:

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

struct Test
{
	int data;
	char cdata;

};

int main()
{
	FILE *fp;

fp=fopen("./file1","w+");

struct Test data1 = {100,'A'};
struct Test data2;


int n_write = fwrite(&data1,sizeof(struct Test),1,fp);


fseek(fp,0,SEEK_SET);


int n_read = fread(&data2,sizeof(struct Test),1,fp);

printf("read:%d %c\n",data2.data,data2.cdata);
fclose(fp);
	

return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值