Linux系统编程_文件

本文详细介绍了Linux系统编程中的文件操作,包括open、read、write和lseek等API的使用,以及综合项目的实践,如实现cp指令、修改配置文件和写入整数。还讨论了fopen、fread、fwrite、fseek、fclose、fgetc、fputc和feof等函数与系统调用的区别。
摘要由CSDN通过智能技术生成

操作系统提供了一系列的API 如Linux系统:

打开       open

读写        write /read

光标定位 lseek    

关闭       close

打开       open

Pathname:要打开的文件名(含路径,缺省为当前路径)

Flags(常数):    

  • O_RDONLY 只读打开        
  • O_WRONLY 只写打开        
  • O_RDWR 可读可写打开    当我们附带了权限后,打开的文件就只能按照这种权限来操作。    
#include <fcntl.h>
#include <stdio.h>

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

	if(fd == -1){
		printf("open file1 failed\n");
		fd = open("./file1",O_RDWR|O_CREAT,0600);
		if(fd > 0){
			printf("create file1 success!\n");
		}
	}


	return 0;
}

     以上这三个常数中应当只指定一 个。下列常数是可选择的: O_CREAT 若文件不存在则创建它。使用此选项时,需要同时说明;

  •  O_EXCL 如果同时指定了OCREAT,而文件已经存在,则出错。              
  •   O_APPEND 每次写时都加到文件的尾端。        
  • O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。

第三个参数mode:用其说明该新文件的存取许可权限。一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限

 

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main()
{
	int fd;
	

	fd = open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);
	if(fd == -1){
		printf("file cunZai\n");
	}


	return 0;
}

读写        read /write

ssize_t read(int fd, void *buf, size_t count);

ssize_t write(int fd, const void *buf, size_t count);

#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 = "chenLichen hen shuai!";	

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

	if(fd == -1){
		printf("open file1 failed\n");
		fd = open("./file1",O_RDWR|O_CREAT,0600);
		if(fd > 0){
			printf("create file1 success!\n");
		}
	}
	
	printf("open susceess : fd = %d\n",fd);
    //写
	int n_write = write(fd,buf,strlen(buf));
	if(n_write != -1){
		printf("write %d byte to file\n",n_write);
	}

	close(fd);

	fd = open("./file1",O_RDWR);
	char *readBuf;
	readBuf = (char *)malloc(sizeof(char)*n_write + 1);	
    //读100个字节
	int n_read = read(fd, readBuf,100);
	
	printf("read %d ,context:%s\n",n_read,readBuf);
	close(fd);

	return 0;
}

光标定位 lseek    

off_t lseek(int fd, off_t offset, int whence);

whence(光标绝对位置):

 SEEK_SET   头

 SEEK_CUR  当前

 SEEK_END   尾
  offset:相对 whence 移动字节数

lseek(fd, -21, SEEK_CUR);

综合项目1:

小项目cp 指令

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
chenlichen
int main(int argc, char **argv)
{
	int fdSrc;
	int fdDes;

	char *readBuf=NULL;

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

	fdSrc = open(argv[1],O_RDWR);
	int size = lseek(fdSrc,0,SEEK_END);
	lseek(fdSrc,0,SEEK_SET);

	readBuf=(char *)malloc(sizeof(char)*size + 8);

	int n_read = read(fdSrc, readBuf, size);
	

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

	
	close(fdSrc);
	close(fdDes);

	return 0;
}

综合项目2:

修改配置文件

修改配置文件 function

#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);
	lseek(fdSrc,0,SEEK_SET);

	readBuf=(char *)malloc(sizeof(char)*size + 8);

	int n_read = read(fdSrc, readBuf, size);
	
	char *p = strstr(readBuf,"LENG=");
	if(p==NULL){
		printf("not found\n");
		exit(-1);
	}
	
	p = p+strlen("LENG=");
	*p = 5;
	
	
	
	lseek(fdSrc,0,SEEK_SET);
	int n_write = write(fdSrc,readBuf,strlen(readBuf));

	
	close(fdSrc);

	return 0;
}

综合项目3:

写入一个整形数;
思路:
1.write/read函数原型buf是一个指针
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;
	
	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 <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;
}

fopen fread fwrite fseek fclose fgetc fputc feof与之前的有何区别

参考博文:总结open与fopen的区别 - NickyYe - 博客园
错误代码:

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

        for(i = 0;i < strlen(str);i++){
                fputc(*str,fp);
                str++;
        }


因为str在偏移,strlen是计算指针指向位置到尾部的长度,所计算出来的值小于真实值;

feof() 判断是否到文件尾巴,当到达文件尾巴时返回值 非0;没到尾巴返回 0;
fgetc() 获取文件内容,使用时fopen 模式要“w”

后面学习都是基于文件基础
小节:熟练应用文件的API(函数),后面深入学习需要到;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值