UC编程04-io读写write/read系统函数的使用

简介: ////myuc.h #include #include #include #include #include #include #include #include #include#include#include...

////myuc.h

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


#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
//#include<sys/types.h>
//#include<sys/stat.h>
#include<string.h>
#include<fcntl.h>
int main(){
	//系统会减去other组的写权限
	int fd=open("open4.txt",O_RDWR|O_CREAT|O_TRUNC,0777);
	if(fd==-1)
		perror("open"),exit(-1);//连接两个语句
	char buf[100]={};
	strcpy(buf,"mybuff1\n");
	write(fd,buf,strlen(buf));
	printf("openok,fd=%d\n",fd);
	close(fd);
	int fd2=open("open4.txt",O_RDONLY);
	if(fd2==-1) perror("open read"),exit(-1);
	memset(buf,0,sizeof(buf));
	int readcount=read(fd,buf,sizeof(buf));
	printf("fd2:%d,readed(%d):%s\n",fd2,readcount,buf);
	close(fd2);
	fd2=open("a.out",O_RDONLY);
	if(fd2==-1) perror("open src"),exit(-1);
	fd=open("a.out1",O_RDWR|O_CREAT|O_TRUNC,0777);
	if(fd==-1) perror("write dest"),exit(-1);
	//readcount=0;
	while(1){
		readcount=read(fd2,buf,sizeof(buf));
		if(readcount==0) break;
		else if(readcount==-1){
			perror("read src");break;
		}
   		write(fd,buf,readcount);
	}
	//while(readcount>0);
	close(fd);
	close(fd2);
	printf("文件复制成功!\n");
	
	return 0;
}

#include "myuc.h"
int main(){
	int fd=open("b.txt",O_RDWR);
	if(fd==-1) perror("open"),exit(-1);
	char ch;
	read(fd,&ch,1);
	printf("%c\n",ch);
	read(fd,&ch,1);
	printf("%c\n",ch);
	lseek(fd,7,SEEK_SET);
	read(fd,&ch,1);
	printf("%c\n",ch);
	lseek(fd,0,SEEK_SET);//回到开始
	write(fd,"1",1);//替换a
	lseek(fd,3,SEEK_SET);
	write(fd,"2",1);//替换d
	lseek(fd,-2,SEEK_END);//注意最后的\n,也是一个字节
	write(fd,"3",1);//替换到数第二个
	int len=lseek(fd,0,SEEK_END);//文件大小
	close(fd);
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
//#include<sys/types.h>
//#include<sys/stat.h>
#include<string.h>
#include<fcntl.h>
typedef struct EMP{
	int id;
	char name[100];
	double salary;
}emp;
int main(){
	int fd=open("write4.txt",O_CREAT|O_RDWR|O_APPEND,0777);
	if(fd==-1) perror("open"),exit(-1);
	printf("请输入编号,姓名,薪水:\n");
	emp em;
	scanf("%d%s%lf",&em.id,em.name,&em.salary);
	int res=write(fd,&em,sizeof(emp));
	if(res==-1) printf("失败\n");
	else printf("成功!\n");
	close(fd);
	fd=open("write4.txt",O_RDONLY);
	while(1)
	{
		res=read(fd,&em,sizeof(em));
		if(res==0) break;
		else if(res==-1) {
			perror("read");break;
		}
		printf("%d,%s,%g\n",em.id,em.name,em.salary);
	}
	close(fd);

	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
void w1()//系统调用多,花时间多
{
	int fd=open("a.txt",O_CREAT|O_TRUNC,0777);
	if(fd==-1) perror("openwrite1"),exit(-1);
	int i;
	for(i=0;i<1000000;i++){
		write(fd,&i,4);
	}
}
void w2(){//系统调用少,省时间
	int fd=open("a.txt",O_RDWR|O_TRUNC);
	if(fd==-1) perror("openwrite2"),exit(-1);
	int arr[10000];
	int i;
	for(i=0;i<1000000;i++){
		arr[i%10000]=i;
		if(i%10000==9999) write(fd,arr,sizeof(arr));//10000个数字才写入一次
	}
	close(fd);
}
int main()
{
	//w1();
	w2();	
}


相关文章
|
4天前
|
存储 网络协议 Linux
【Linux】进程IO|系统调用|open|write|文件描述符fd|封装|理解一切皆文件
本文详细介绍了Linux中的进程IO与系统调用,包括 `open`、`write`、`read`和 `close`函数及其用法,解释了文件描述符(fd)的概念,并深入探讨了Linux中的“一切皆文件”思想。这种设计极大地简化了系统编程,使得处理不同类型的IO设备变得更加一致和简单。通过本文的学习,您应该能够更好地理解和应用Linux中的进程IO操作,提高系统编程的效率和能力。
52 34
|
22天前
|
存储 弹性计算 固态存储
阿里云服务器ESSD Entry系统盘测评IOPS、IO读写和时延性能参数
阿里云ESSD Entry云盘是新一代企业级云盘,具备高IOPS、低延迟特性,适合开发与测试场景。它提供10~32,768 GiB容量范围,最大IOPS达6,000,吞吐量150 MB/s,时延1~3 ms。支持按量付费和包年包月,性价比高,特别适合个人开发者和中小企业。详情及价格参考阿里云官网。
|
4月前
|
数据库
同步IO模型是一种常见的编程模型
【10月更文挑战第5天】同步IO模型是一种常见的编程模型
29 2
|
3月前
|
存储 弹性计算 固态存储
阿里云服务器ESSD Entry系统盘测评IOPS、IO读写和时延性能参数
ESSD Entry云盘是阿里云推出的新一代云盘,具备高IOPS、低延迟和企业级数据保护能力。适用于开发与测试场景,支持按量付费和包年包月计费模式。99元和199元的ECS经济型e实例和通用算力型u1实例均采用ESSD Entry系统盘,性价比高。详细性能参数和价格请参考阿里云官方页面。
149 0
|
5月前
|
网络协议 Java Linux
高并发编程必备知识IO多路复用技术select,poll讲解
高并发编程必备知识IO多路复用技术select,poll讲解
|
6月前
|
Java 数据处理
Java IO 接口(Input)究竟隐藏着怎样的神秘用法?快来一探究竟,解锁高效编程新境界!
【8月更文挑战第22天】Java的输入输出(IO)操作至关重要,它支持从多种来源读取数据,如文件、网络等。常用输入流包括`FileInputStream`,适用于按字节读取文件;结合`BufferedInputStream`可提升读取效率。此外,通过`Socket`和相关输入流,还能实现网络数据读取。合理选用这些流能有效支持程序的数据处理需求。
76 2
|
6月前
|
Ubuntu Linux
内核实验(九):添加IO驱动的阻塞读写功能
本文通过修改内核模块代码,介绍了如何在Linux内核中为IO驱动添加阻塞读写功能,使用等待队列和条件唤醒机制来实现读写操作的阻塞和非阻塞模式,并在Qemu虚拟机上进行了编译、部署和测试。
37 0
|
7月前
|
缓存 网络协议 算法
【Linux系统编程】深入剖析:四大IO模型机制与应用(阻塞、非阻塞、多路复用、信号驱动IO 全解读)
在Linux环境下,主要存在四种IO模型,它们分别是阻塞IO(Blocking IO)、非阻塞IO(Non-blocking IO)、IO多路复用(I/O Multiplexing)和异步IO(Asynchronous IO)。下面我将逐一介绍这些模型的定义:
370 2
|
8月前
|
Java 数据库连接
提升编程效率的利器: 解析Google Guava库之IO工具类(九)
提升编程效率的利器: 解析Google Guava库之IO工具类(九)
|
9月前
|
存储 Java API
Java语言IO(输入/输出)编程技术深度解析
Java语言IO(输入/输出)编程技术深度解析
310 1

热门文章

最新文章