操作系统基本的接口函数

文件描述符:

操作系统通过一个整数代表打开的文件,这个整数就被称为文件描述符,windows上称为文件的句柄,进程能够打开的文件描述符的范围[0,1024] ,我们可以用ulimit-n查看。有三个文件描述符进程一创建时就打开的

标准输入stdin:0              标准输出stdout:1             标准错误stderroe:2 

操作系统提供的基本接口函数:open 、read、write、close

open函数:(两个功能,打开文件、创建文件)

1.int open(const char *path,int flags);

path为要打开的文件,flags为打开方式(读/写/读写),功能为打开文件,失败返回-1,成功则返回文件描述符的值

O_RDONLY:读方式

O_WRONLY:写方式

O_RDWR:读写方式

具体使用代码:

  1 #include<stdlib.h>
  2 #include<stdio.h>
  3 #include<unistd.h>
  4 #include<fcntl.h>
  5 int main()
  6 {
  7     int fd=open("open.c",O_RDONLY);
  8     if(fd==-1)
  9     {
 10         perror("open");
 11     }
 12     else
 13     {
 14         printf("ok!");                                                      
 15     }
 16     close(fd);
 17 }

2.int open(const char*path,int flags,mode_t mode)

mode为创建文件所赋予的权限一般为0644,flags:O_CREAT|三种方式|O_EXCL,path为要创建的文件名

O_EXCL只能和O_CREAT连用,以确保不会创建重复的文件

O_TRUNC:打开文件后清空文件

O_APPEND:往文件中追加内容

此外我们还要知道:创建文件权限要受到umask影响,系统重复创建文件不会覆盖原来的文件内容

具体使用代码:

  1 #include<stdio.h>
  2 #include<unistd.h>
  3 #include<fcntl.h>                                                           
  4 #include<stdlib.h>
  5 int main()
  6 {
  7     int fd=open("hello",O_CREAT|O_RDWR,0644);
  8     if(fd==-1)
  9     {
 10         perror("open");
 11         exit(1);
 12     }
 13     else
 14     {
 15         printf("creat success");
 16     }
 17     close(fd);
 18 }

read函数:

int  read(int fd,char *buf,size_t len);

fd为文件描述符,读取到buf所指向的空间,空间的大小为len,返回值为实际读取的字节数。并非一定要读够len,当小于len时读够就结束,失败返回-1,读取到文件结尾返回0

具体使用代码:

 1 #include<stdio.h>                                                           
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<unistd.h>
  5 #include<fcntl.h>
  6 int main()
  7 {
  8     int fd=open("read.c",O_RDONLY);
  9     if(fd==-1)
 10     {
 11         perror("open");
 12         exit(1);
 13     }
 14     else
 15     {
 16         while(1)
 17         {
 18             char buf[20]={};
 19             int r=read(fd,buf,10);
 20             if(r==-1)
 21             {
 22                 perror("read");
 23                 exit(1);
 24             }
 25             else if(r==0)
 26             {
 27                 printf("finsh read");
 28                 break;
 29             }
 30             else
 31             {
 32                 printf("[%s]\n",buf);
 33             }
 34         }
 35     }
 36     close(fd);
 37 }                                                                           

write函数:

int write (int fd,const char *buf,size_t len);

往fd所指向的文件中写入数据,数据的起始地址为buf,大小为len,失败返回-1,正确返回写入的字节数

具体使用代码:

  1 #include<stdio.h>                                                           
  2 #include<unistd.h>
  3 #include<string.h>
  4 #include<fcntl.h>                                                       
  5 #include<stdlib.h>
  6 int main()
  7 {
  8     int fd=open("hello",O_CREAT|O_RDWR,0644);
  9     if(fd==-1)
 10     {
 11         fd=open("hello",O_RDWR);
 12         if(fd==-1)
 13         {
 14             perror("open");
 15             exit(1);
 16         }
 17     }
 18         char *msg="this is enjoyselflzz";
 19         int r=write(fd,msg,strlen(msg));
 20         if(r==-1)
 21         {
 22             perror("write");
 23         }
 24         else
 25         {
 26             printf("len=%d,r=%d\n",strlen(msg),r);
 27         }
 28          close(fd);
 29 }

close函数:

int close(int fd)

关闭文件,为了释放资源,如果不close()那就要等到垃圾回收时,自动释放,但垃圾回收的实时机是不确定的,也无法控制的。如果程序是一个服务,或者时需要很长时间才能执行完,或者很大并发执行,就可能导致资源被耗尽,也可能导致死锁。

温馨提示:open()完记得要close()!

操作文件的接口函数:

FILE*fp=fopen(“文件名”,“方式rwa+”)打开文件

fgetc(fp)表示从文件里读取一个字符------------------------------------>fputc()

fgets(buf,len,fp)从文件中读取一行-------------------------------------->fputs()

fscanf(fp,"%s",buf)从文件里按照给定的格式读取内容装入buf中------------------------------------->fprintf()

fread(buf,size,nnumb,fp) buf是读入的文件,size是一个元素的大小,nnum是元素个数,fp所读的文件---------> fwrite()

fclose(fp)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值