系统编程第二天

一、write

实时检测文件的命令:tailf

文件关闭后,再次开启每次默认从文件开头开始00

头文件
#include <unistd.h>
格式
ssize_t write(int fd, const void *buf, size_t count);;
第一个参数:写的文件的文件描述符
第二个参数:存放在缓冲区的要写入的数据
第三个参数:想要写的大小(字节)
返回值
成功:返回实际写到文件中的字节数(可能和前面形参中的count不同)
失败:返回-1

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

#define SIZE 1024

int main1()			//写入一串字符串
{
	int fd = open("tmp.txt",O_WRONLY | O_CREAT | O_TRUNC,0766);
	if(-1 ==fd)
	{
		perror("打开文件失败");
		return -1;
	}
	
	ssize_t ret = write(fd,"A good boy",10);
	if(-1 == ret)
	{
		perror("写入文件失败");
		return -1;
	}
	
	close(fd);
	
	return 0;
}

int main2()			//将键盘输入的内容写到文件中,以end结束
{
	int fd = open("tmp.txt",O_WRONLY | O_CREAT | O_APPEND,0766);
	if(-1 ==fd)
	{
		perror("打开文件失败");
		return -1;
	}
	
	char buf[SIZE];
	
	while(1)
	{
		fgets(buf,SIZE,stdin);
		
		if(strncmp(buf,"end",3) == 0)
			break;
		ssize_t ret = write(fd,buf,strlen(buf));
		if(-1 == ret)
		{
			perror("写入文件失败");
			return -1;
		}
	}
	
	close(fd);
	
	return 0;
}

int main3()			//实现文件的拷贝
{
	int fd1 = open("1.ppt",O_RDONLY);
	int fd2 = open("2.ppt",O_WRONLY | O_CREAT,0766);
	if(-1 ==fd1 || -1 == fd2)
	{
		perror("打开文件失败");
		return -1;
	}
	
	char buf[SIZE];
	
	ssize_t ret;
	while((ret = read(fd1,buf,SIZE)) != 0)
	{
		if(-1 == ret)
		{
			perror("读文件失败");
			return -1;
		}
		
		ret = write(fd2,buf,ret);
		if(-1 == ret)
		{
			perror("写入文件失败");
			return -1;
		}
	}
	
	close(fd1);
	close(fd2);
	
	return 0;
}

int main4()			//写入整型数据
{
	int fd = open("tmp.txt",O_WRONLY | O_CREAT | O_TRUNC,0766);
	if(-1 ==fd)
	{
		perror("打开文件失败");
		return -1;
	}
	
	int a = 20;
	ssize_t ret = write(fd,&a,sizeof(int));
	if(-1 == ret)
	{
		perror("写入文件失败");
		return -1;
	}
	
	close(fd);
	
	return 0;
}

int main()			//读整型数据
{
	int fd = open("tmp.txt",O_RDONLY);
	if(-1 ==fd)
	{
		perror("打开文件失败");
		return -1;
	}
	
	int a;
	ssize_t ret = read(fd,&a,sizeof(int));
	if(-1 == ret)
	{
		perror("读文件失败");
		return -1;
	}
	
	printf("a = %d\n",a);
	
	close(fd);
	
	return 0;
}

二、lseek

改变文件指针的位置

创建大文件
1、dd命令,创建大文件
dd if=/dev/zero of ./文件名 bs=文件大小 count =几个
2、用lseek
3、函数truncate(文件必须存在)

头文件
#include <sys/types.h>
#include <unistd.h>

格式
off_t lseek(int fd, off_t offset, int whence);
第一个参数:想要改的文件
第二个参数:移动多少个字节
第三个参数:开始的方式,从当前位置: SEEK_CUR,从起始位置: SEEK_SET,从结尾:SEEK_END

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

#define SIZE 1024*1024*40

int main1()
{
	int fd = open("1.ppt",O_WRONLY); 
	if(-1 == fd)
	{
		perror("打开文件失败");
		return -1;
	}
	
	off_t ret = lseek(fd,0,SEEK_END);

	printf("文件大小:%ld\n",ret);
	
	return 0;
}

int main2()
{
	int fd = open("tmp.txt",O_WRONLY |  O_TRUNC); 
	if(-1 == fd)
	{
		perror("打开文件失败");
		return -1;
	}
	
	write(fd,"I am a",6); 
	//off_t ret = lseek(fd,0,SEEK_END);
	lseek(fd,5,SEEK_SET);
	write(fd,"good boy",8); 
	
	return 0;
}

//创建大文件
int main3()
{
	int fd = open("tmp.txt",O_WRONLY |  O_TRUNC); 
	if(-1 == fd)
	{
		perror("打开文件失败");
		return -1;
	}
	
	lseek(fd,SIZE-1,SEEK_SET);
	write(fd,"g",1);
	
	return 0;
}

//创建大文件
int main()
{
	truncate("big",SIZE);
	
	return 0;
}

三、文件描述符

1、每次打开文件都会返回一个新的文件描述符,每次偏移量都是从头开始的

2、dup:复制文件描述符,共用一个文件偏移量
dup2:dup(fd,newfd),将文件描述符表中的地址复制一份给newfd,如果复制成功,会将原来newfd打开的文件关闭
在这里插入图片描述

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

int main1()
{
	int fd = open("test.txt",O_WRONLY | O_CREAT);
	if (-1 == fd)
	{
		perror("打开文件失败");
		return -1;
	}
	
	int newfd = dup(fd);
	
	printf("fd = %d,newfd = %d\n",fd,newfd);
	
	write(fd,"hello",5);
	write(newfd,"world",5);
	
	return 0;
}


int main2()
{
	int fd = open("test.txt",O_WRONLY | O_CREAT |  O_TRUNC);
	int newfd = open("test.txt",O_WRONLY);
	if (-1 == fd || -1 == newfd)
	{
		perror("打开文件失败");
		return -1;
	}
	
	//int newfd = dup(fd);
	
	printf("fd = %d newfd = %d\n",fd,newfd);
	
	write(fd,"hello",5);
	write(newfd,"world",5);
	
	return 0;
}

int main()
{
	int fd = open("test.txt",O_WRONLY | O_CREAT |  O_TRUNC);
	int newfd = open("test2.txt",O_WRONLY | O_CREAT);
	if (-1 == fd || -1 == newfd)
	{
		perror("打开文件失败");
		return -1;
	}
	
	dup2(fd,newfd);
	
	printf("fd = %d newfd = %d\n",fd,newfd);
	
	write(fd,"hello",5);
	write(newfd,"world",5);
	
	return 0;
}

四、缓冲区

在这里插入图片描述
缓冲区:提高效率,减少用户区和内核区平凡切换
输出缓冲区导出条件:
1、遇到换行
2、遇到输入操作
3、缓冲区满:4k????
4、手动清空fflush(文件指针)
5、程序结束会自动清空缓冲区

六、文件操作

C库,打开文件fopen
打开方式
1、“r”或者“rb”:只读
2、“w”或者“wb”:只写,并把文件长度阶短为0(清空)
3、“a”或者“ab”:只写,新内容追加在文件尾部
4、"r+"或者“rb+”或者“r+b”:读写方式打开(更新方式)
4、“w+”或者“wb+”或者“w+b”:读写方式打开,并把文件长度阶短为0(清空)
5、“a+”或者“ab+”或者“a+b”:更新方式打开,新内容追加在文件尾部
注:字母表示是一个二进制文件(没有编码的文件)而不是文本文件
fread的格式
FILE *fopen(const char *path, const char *mode);
第一个参数:打开的文件名
第二个参数:打开的方式
返回值:文件指针

七、fread

fread的格式 ,fwrite的格式
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
第一个参数:缓冲区
第二个参数:想要读的类型
第三个参数:读几个这种类型的长度
第四个参数:文件对应的指针
返回值:读到达这种类型的个数
返回错误和结束都是0,还需要用feod判断
Feof
返回0 说明错误
非0说明读完了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值