栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Linux之文件编程(个人理解总结)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux之文件编程(个人理解总结)

师承------陈立臣

文章目录
  • 文件的类型
  • 一、文件操作
  • 二、自行实现cp命令
    • 巧用lseek计算文件大小
  • 三、如何写一个整数到文件?
  • 四、如何写一个结构体或者结构体数组到文件?
  • 五、标准c库的打开/关闭/文件定位/写文件/读文件
  • 六、标准c库对文件的操作示例
    • fgetc feof的使用
  • 七、fopen与open的区别
    • 1.来源
    • 2.移植性
    • 3.缓冲


文件的类型

1.普通文件 -
2.目录文件 d
3.字符设备文件 c
4.块设备文件 b
5.管道文件 f
6.链接文件 l
7.socket文件 s
每个文件分为:索引节点(inode) 和文件数据块
索引节点:记录文件的各种属性信息
文件的数据块:保存文件的具体内容


一、文件操作

打开/创建文件


关闭文件

文件定位

读文件

写文件

二、自行实现cp命令

思路:
1.打开源文件
2.读出源文件的数据
3.打开目标文件
4.向目标文件写入数据
5.关闭文件描述符
代码如下(示例):

#include
#include 
#include 
#include  
#include 
#include


int main(int argc,char* argv[])
{
	int fd;
	int fd1;
	int filesize;
	char* readbuf=NULL;
	if(argc<3){
		printf("the parm is no corect!n");
		return -1;
	}
	fd=open(argv[1],O_RDWR);
	if(fd<0){
		printf("open %s failed!n",argv[1]);
	}

	filesize=lseek(fd,0,SEEK_END);
	readbuf=(char*)malloc(sizeof(char)*filesize+5);
	lseek(fd,0,SEEK_SET);
	read(fd,readbuf,filesize);
	fd1=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
	if(fd1<0){
		printf("open %s failed!n",argv[1]);
		return -1;
	}
	int n_write=write(fd1,readbuf,filesize);
	printf("write ok the byte is %dn",n_write);
	close(fd1);

	close(fd);
	return 0;

}
巧用lseek计算文件大小

代码如下(示例):

lseek(fd,0,SEEK_END);

三、如何写一个整数到文件?
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main()
{
	int fd;
	
	int data = 100;
	int data2 = 0;

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

	int n_write = write(fd,&data,sizeof(int));

	lseek(fd,0,SEEK_SET);

	int n_read = read(fd, &data2, sizeof(int));
	
	printf("read %d n",data2);
	close(fd);

	return 0;
}

四、如何写一个结构体或者结构体数组到文件?

写入结构体到文件:

#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);

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


写入结构体数组到文件

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


五、标准c库的打开/关闭/文件定位/写文件/读文件



六、标准c库对文件的操作示例
#include 
#include 

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

	FILE *fp;
	char *str = " hen shuai";
	char readBuf[128] = {0};

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

	//size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
	//ptr  buf
	//size  sizeof char   
	// geshu 
	// which file
	fwrite(str,sizeof(char),strlen(str),fp);
//	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);
	fread(readBuf,sizeof(char),strlen(str),fp);
	
	printf("read data: %sn",readBuf);

	return 0;
}

fgetc feof的使用
#include 
#include 


int main()
{
	FILE *fp;
	int i;
	char c;

	fp = fopen("./test.txt","r");

	while(!feof(fp)){// nonezero if reach end of file
		
		c = fgetc(fp);
		printf("%c",c);
	}
	fclose(fp);
	return 0;
}

七、fopen与open的区别 1.来源

open是Linux系统调用的函数,返回文件描述符
fopen 是标准c库,返回的是文件结构体指针

2.移植性

fopen来源于标准c库,移植性很强,而open只能在Linux系统下使用,移植性稍差

3.缓冲

open无缓冲,fopen有缓冲。前者与read, write等配合使用, 后者与fread,fwrite等配合使用
点此博文,了解更加详细

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1020441.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号