linux gnu文件操作

例子1 拷贝文件
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main(){
  int fd1 = open("a.txt",O_RDONLY);
  if(fd1 == -1) perror("open a"),exit(-1);
  int fd2=open("b.txt",O_RDWR|O_CREAT|O_TRUNC,0666);
  if(fd2 == -1) perror("open b"),exit(-1);
  char buf[3] = { };//真正开发时,长度2k或4k
  while(1){//如果文件比较大,一次读不完
    int res = read(fd1,buf,sizeof(buf));//读a
    if(res == -1) { perror("read"); break; }
	if(res == 0) { printf("拷贝完毕\n"); break; }
	write(fd2,buf,res);//读多少,就写多少
  }//多读一次,读的是文件尾,返回0
  close(fd1); close(fd2);
}

例子2 打开文件

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int main(){
  printf("O_CREAT=%d,O_RDONLY=%d,O_WRONLY=%d,"
    "O_RDWR=%d,O_APPEND=%d\n",O_CREAT,O_RDONLY,
    O_WRONLY,O_RDWR,O_APPEND);
  int fd =open("a.txt",O_RDWR|O_CREAT|O_TRUNC,0666);

  if(fd == -1) perror("open"),exit(-1);
  printf("fd=%d\n",fd);
  close(fd);
}//"abc""123" == "abc123"

例子3 读文件

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main(){
  int fd = open("a.txt",O_RDONLY);
  if(fd == -1) perror("open"),exit(-1);
  char buf[100] = { };
  int res = read(fd,buf,sizeof(buf));
  if(res == -1) perror("read"),exit(-1);
  printf("读到了%d字节数据,内容:%s\n",res,buf);
  close(fd);
}


文件描述符复制


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main(){
  int fd = open("a.txt",O_RDWR);
  if(fd == -1) perror("open"),exit(-1);
  int fd2 = dup(fd);//只复制描述符,不复制文件表
  printf("fd=%d,fd2=%d\n",fd,fd2);
  write(fd,"1",1);
  write(fd2,"2",1);
  int fd3 = dup2(fd,100);
  printf("fd3=%d\n",fd3);//100
  int fd4 = open("b.txt",O_RDWR|O_CREAT,0666);
  printf("fd4=%d\n",fd4);//5
  int fd5 = dup2(fd,5);//关闭b.txt,打开a.txt
  printf("fd5=%d\n",fd5);//5
  write(fd5,"HEHE",4);//共用文件表,在2的后面写
  close(fd);//从文件描述符总表删除fd
}

fnctl

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main(){
  int fd=open("a.txt",O_RDONLY|O_CREAT|O_APPEND,
	0666);
  if(fd == -1) perror("open"),exit(-1);
  int flags = fcntl(fd,F_GETFL);//取不到O_CREAT
  printf("flags=%d\n",flags);
  //验证某一位二进制是0还是1,用 位与& 运算
  if(flags & O_CREAT) printf("CREATE\n");
  if(flags & O_APPEND) printf("APPEND\n");
  //权限比较用后两位,如何取后2位,与3位与
  if((flags&3)== O_RDWR) printf("RDWR\n");
  if((flags&3)== O_RDONLY) printf("RDONLY\n");
  printf("------------------\n");
  fcntl(fd,F_SETFL,O_RDWR);//设置,只有APPEND能改
  flags = fcntl(fd,F_GETFL);//取出新的状态
  if(flags & O_APPEND) printf("APPEND\n");
  if((flags&3)== O_RDWR) printf("RDWR\n");
  if((flags&3)== O_RDONLY) printf("RDONLY\n");
  close(fd);
}

读写文件锁

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main(){
  int fd = open("a.txt",O_RDWR);
  if(fd == -1) perror("open"),exit(-1);
  struct flock lock;
  lock.l_type = F_RDLCK;//读锁  写锁是F_WRLCK
  lock.l_whence = SEEK_SET;//从头开始最简单
  lock.l_start = 0;//从文件的第一个字节开始锁定
  lock.l_len = 20;//锁定20个字节
  lock.l_pid = -1;//SETLK用不到这个参数,-1即可
  int res = fcntl(fd,F_SETLK,&lock);//上锁
  if(res == -1) perror("上锁失败\n"),exit(-1);
  else printf("成功了\n");
  sleep(20);
  printf("读文件完成了,释放读锁\n");
  lock.l_type = F_UNLCK;
  fcntl(fd,F_SETLK,&lock);//释放锁的代码
  sleep(20);
  printf("其他代码也完毕,程序结束\n");
  close(fd);
}


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main(){
  int fd = open("a.txt",O_RDWR|O_TRUNC);
  if(fd == -1) perror("open"),exit(-1);
  struct flock lock;
  lock.l_type = F_WRLCK;
  lock.l_whence = SEEK_SET; lock.l_start = 0;
  lock.l_len = 10; lock.l_pid = -1;
  int res = fcntl(fd,F_SETLKW,&lock);
  if(res == -1){
    printf("有进程在使用,无法写文件\n");
  }else{
    res = write(fd,"hello",5);//写文件前先加写锁
    if(res == -1) perror("write"),exit(-1);
    else printf("写入成功\n");
  }
  close(fd);
}




lseek

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main(){
  int fd = open("a.txt",O_RDWR);
  if(fd == -1) perror("open"),exit(-1);
  char ch;
  read(fd,&ch,1); printf("%c\n",ch);//a
  read(fd,&ch,1); printf("%c\n",ch);//b
  lseek(fd,3,SEEK_CUR);
  read(fd,&ch,1); printf("%c\n",ch);//f 
  lseek(fd,0,SEEK_SET);//回到开始
  write(fd,"1",1);//a
  lseek(fd,3,SEEK_SET); write(fd,"2",1);//d  
  lseek(fd,2,SEEK_CUR); write(fd,"3",1);//g
  lseek(fd,-2,SEEK_CUR); write(fd,"4",1);//f 
  lseek(fd,-3,SEEK_END); write(fd,"5",1);//s
  close(fd);
}

mmap

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
struct Emp{
  int id;
  char name[20];
  double sal;
};
int main(){
  int fd=open("em.dat",O_RDWR|O_CREAT|O_TRUNC,0666);
  if(fd == -1) perror("open"),exit(-1);
  void* p = mmap(0,sizeof(struct Emp)*3,
	PROT_READ|PROT_WRITE,
	//MAP_SHARED,//数据存入文件
	MAP_PRIVATE,//数据不存入文件
	fd,0);
  if(p == MAP_FAILED) perror("mmap"),exit(-1);
  ftruncate(fd,sizeof(struct Emp)*3);//指定大小
  struct Emp* pe = p;//数据在文件中
  pe[0].id = 100; strcpy(pe[0].name,"zhangfei");
  pe[0].sal = 12000.0;
  pe[1].id = 200; strcpy(pe[1].name,"guanyu");
  pe[1].sal = 20000.0;
  pe[2].id = 300; strcpy(pe[2].name,"liubei");
  pe[2].sal = 24000.0;
  int i;
  for(i=0;i<3;i++)
    printf("%d,%s,%lf\n",pe[i].id,pe[i].name,
	  pe[i].sal);
  close(fd);
}


打印目录

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

void print(char* path){//把代码写一下
  DIR* dir = opendir(path);
  if(dir == NULL) return;//结束递归但不结束进程
  struct dirent* ent;
  while(ent=readdir(dir)){//先read,后赋值,再判断
	if((strcmp(ent->d_name,".")==0) || 
	  (strcmp(ent->d_name,"..")==0)) continue;
	if(ent->d_type == 4){//子项是目录
      printf("[%s]\n",ent->d_name);//dname不包括路径
	  char buf[100] = {};
	  sprintf(buf,"%s/%s",path,ent->d_name);
	  print(buf);//递归调用打印子目录
	}else{//子项是 文件
	  printf("%s\n",ent->d_name);
	}
  }
}
int main(){
  print("../"); 
  //显示UC目录下的所有子项,子目录的内容也要显示出来
  //思路:先读UC目录的子项,如果子项是目录,打印出来
  //然后递归调用print(),如果子项是文件,直接打印
}

umask
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){//了解即可
  int fd1 = open("aa",O_RDWR|O_CREAT,0666);//664
  mode_t old = umask(0022);//属主不屏蔽,其他屏蔽2
  int fd2 = open("bb",O_RDWR|O_CREAT,0666);//644
  umask(old);//还原成系统默认屏蔽
  int fd3 = open("cc",O_RDWR|O_CREAT,0666);//664
  close(fd1); close(fd2); close(fd3);
}

stat
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
//取文件大小必须会
int main(){
  struct stat s = {};
  int res = stat("a.txt",&s);
  if(res == -1) perror("stat"),exit(-1);
  printf("获取硬盘文件的属性成功\n");
  printf("inode=%d\n",s.st_ino);
  printf("size=%d\n",s.st_size);
  printf("nlink=%d\n",s.st_nlink);
  printf("tm=%s\n",ctime(&s.st_mtime));//转英文日期
  printf("mode=%o\n",s.st_mode);
  printf("权限:%o\n",s.st_mode & 07777);//取后4位
  if(S_ISREG(s.st_mode)) //系统提供宏函数判断类型
	printf("普通文件\n");
  if(S_ISDIR(s.st_mode))
	printf("目录\n");
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
操作系统的定义:是一组控制和管理计算机软硬件资源,为用户提供便捷使用计算机的程序的集合。 基本功能:1.进程管理 2.存储管理 3.文件管理 4.设备管理 5.作业管理 基本组成: 1、驱动程序 最底层bai的、直接控制和监视各类硬件的部分,它du们的职zhi责是隐藏硬件的具体细节,并向dao其他部分提供一个抽象的、通用的接口。 2、内核 操作系统之最内核部分,通常运行在最高特权级,负责提供基础性、结构性的功能。 3、支承库(亦作“接口库”) 是一系列特殊的程序库,它们职责在于把系统所提供的基本服务包装成应用程序所能够使用的编程接口(API),是最靠近应用程序的部分。例如,GNU C运行期库就属于此类,它把各种操作系统的内部编程接口包装成ANSI C和POSIX编程接口的形式。 4、外围 所谓外围,是指操作系统中除以上三类以外的所有其他部分,通常是用于提供特定高级服务的部件。例如,在微内核结构中,大部分系统服务,以及UNIX/Linux中各种守护进程都通常被划归此列。 操作系统的分类:1.批处理系统 2.分时操作系统 3.实时操作系统 4.分布式操作系统 5.网络操作系统 6.嵌入式操作系统 操作系统的特点: 1、并发性: 是在计算机bai系统中同时存在多个程序,宏观上看,du这些程序是同时向前推进的。 在单CPU上,这些并发执行的程序是交替在CPU上运行的。 程序并发性体现在两个方面: 用户程序与用户程序之间的并发执行。 用户程序与操作系统程序之间的并发。 2、共享性: 资源共享是操作系统程序和多个用户程序共用系统中的资源。 3、 随机性: 随机性指:操作系统的运行是在一个随机的环境中,一个设备可能在任何时间向处理机发出中断请求,系统无法知道运行着的程序会在什么时候做什么事情。 4、虚拟 (virtual)是指通过技术将一个物理实体变成若干个逻辑上的对应物。在操作系统中虚拟的实现主要是通过分时的使用方法。显然,如果n是某一个物理设备所对应的虚拟逻辑设备数,则虚拟设备的速度必然是物理设备速度的1/n。 5、异步性:不确定性。同一程序和数据的多次运行可能得到不同的结果;程序的运行时间、运行顺序也具有不确定性;外部输入的请求、运行故障发生的时间难以预测。这些都是不确定性的表现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值