文件IO编程

1.一切皆文件

主要分为4种:普通文件,目录文件,链接文件,设备文件。涉及的函数,open,read,write,ioctl,close。目录则是opendir/readdir

2.文件描述符

文件描述符就是一个非负整数,是一个索引值,指向内核中每个进程打开的记录表。一个进程的启动,都会打开3个文件,标准输入,标准输出,标准出错,对应的文件描述符分别为位0,1,2,宏定义为(STDIN_FILENO,STDOUT_FILENO,STDERR_FILENO)

3.不带缓存IO操作

1.主要涉及到open,close,read,write,lseek这5个函数。不能移植到非posix标准系统上面
2.关于函数参数,如何获取?
一.man open(man 2 xx,man open查到的不是我想要的,man有9册,默认第一册,可以先man -f open,然后man 1 open或者man 2 open(取决于第一册还是第二册的))
二.baidu/google
三.看书,看源码

   #include <sys/types.h>//man查看提示需要添加的
   #include <sys/stat.h>
   #include <fcntl.h>
   
   #include <unistd.h>//类似windows.h
   
   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>

   int main(int argc,char **argv)
   {
       int fd,size;
       char *buf="hello,i am liu,This is file io test";
       char buf_r[20];
       int len=strlen(buf);
/*
O_RDONLY 以只读方式打开文件,与 O_WRONLY 和 O_RDWR 互斥
O_WRONLY 以只写方式打开文件,与 O_RDONLY 和 O_RDWR 互斥
O_RDWR   以可读写方式打开文件,与 O_WRONLY 和 O_RDONLY 互斥
O_CREAT  如果要打开的文件不存在,则创建该文件
O_EXCL   该标志与 O_CREAT 共同使用时,会去检查文件是否存在,若文件不存
在则创建该文件,否则将导致打开文件失败。此外,打开文件链接时,使用该标志将导致失败
O_NOCTTY 如果要打开的文件为终端设备,则不把该终端设备当成控制终端
O_TRUNC若文件存在且以可写方式打开,此标志会清除文件内容,并将其长度置为 0
O_APPEND读写文件都从文件的尾部开始,所写入的数据会以追加的方式插入到文件末尾
O_NONBLOCK以不可阻塞方式打开文件,也就是不管有无数据需要读写或者等待,都会立即返回
O_NDELAY以不可阻塞方式打开文件,也就是不管有无数据需要读写或者等待,都会立即返回(已过时,由 O_NONBLOCK 替代)
O_SYNC 以同步方式打开文件
O_NOFOLLOW 如果文件名所指向的文件本身为符号链接,则会导致打开文件失败
O_DIRECTORY 如果文件名所指向的文件本身并非目录,则会导致打开文件失败
*/
  //可查看全局变量errno的值  
     if((fd=open("./hello.c",O_CREAT|O_TRUNC|O_RDWR,0666))<0)//指定读写权限,只有创建时候第三个参数才起作用
       {
           printf("open fail\r\n");
           exit(1);
       }
       else
       {
           printf("hello.c open successed,fd=%d\r\n",fd);
       }

       if((size=write(fd,buf,len))<0)
       {
           printf("write fail\r\n");
           exit(1);
       }
       else
       {
           printf("write : %s\r\n",buf);
           //针对嵌入式存储,加入fsync(fd)进行同步。而sync()是针对整个系统的,消耗时间长
       }

       lseek(fd,0,SEEK_SET);//移动文件指针到开头
       if((size=read(fd,buf_r,15))<0)
       {
           printf("read fail\r\n");
           exit(1);
       }
       else
       {
           buf_r[15]='\0';
           printf("read successed %s\r\n",buf_r);
       }
       if(close(fd)<0)//关闭文件
       {
           printf("close fail\r\n");
           exit(1);
       }
       else
       {
           printf("close successed\r\n");
       }
   }

结果:

hello.c open successed,fd=3
write : hello,i am liu,This is file io test
read successed hello,i am liu,
close successed

4.带缓存IO操作

也就是标准的C语言IO操作。还有setbuf,setvbuf修改缓冲区的工作模式,一开始就要设置,这样可以避免掉电数据没写入

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

       int main(int argc,char **argv)
       {
           FILE *fp;
           char *buf="hello,i am liu,This is file io test";
           char buf_r[20];
           int len=strlen(buf);

           if((fp=fopen("./hello.c","w+"))!=NULL)//can read and write
           {
               printf("open successed\r\n");
           }
           

           if((fwrite(buf,sizeof(char),len,fp))>0)
           {
              printf("write : %s\r\n",buf);
               
           }
           

           fseek(fp,0,SEEK_SET);
           if((fread(buf_r,sizeof(char),15,fp))<0)
           {
               printf("read fail\r\n");
               exit(1);
           }
           else
           {
               buf_r[15]='\0';
               printf("read successed %s\r\n",buf_r);
           }
           if(fclose(fp)<0)
           {
               printf("close fail\r\n");
               exit(1);
           }
           else
           {
               printf("close successed\r\n");
           }
       }

结果:

open successed
write : hello,i am liu,This is file io test
read successed hello,i am liu,
close successed
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值