孤狼默戮

不断学习,不断的发现自己无知

Itop4412学习笔记(2)

0
阅读(1897)

今天学习的是文件IO的操作,需要记录的点:

1.库函数头文件

    在所有Linux系统中,对文件的操作都只需包含下面四个头文件即可:

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

2.打开文件函数open

    open函数会返回一个文件句柄,下面是函数open的两种原型: 

    int open(const char *path,int oflags); 

    int open(const char *path,int oflags,mode_t mode);

    open函数有2到3个参数,其返回值为对应文件的句柄:

    1.第一个参数path为绝对路径

    2.第二个参数oflags为读写方式等操作

    • 下面三个选项是必须选择其中之一的。

       O_RDONLY 文件只读   
       O_WRONLY 文件只写   
       O_RDWR 文件可读可写
    • 下面是可以任意选择的。

       O_APPEND 每次写操作都写入文件的末尾   
       O_CREAT 如果指定文件不存在,则创建这个文件   
       O_EXCL 如果要创建的文件已存在,则返回-1,并且修改errno 的值   
       O_TRUNC 如果文件存在,并且以只写/读写方式打开,则清空文件全部内容   
       O_NOCTTY 如果路径名指向终端设备,不要把这个设备用作控制终端。   
       O_NONBLOCK 如果路径名指向FIFO/块文件/字符文件,则把文件的打开和后继I/O设置为非阻塞模式   O_NDELAY 和O_NONBLOCK 功能类似,调用O_NDELAY 和使用的O_NONBLOCK 功能是一样的。

    3.第三个参数mode为设置创建文件的权限。

      其参数设置可参照linux下的权限值,例如chmod 777 filename

    下面是一个open的测试例程:

    #include <stdio.h> 
    #include <unistd.h> 
    #include <sys/types.h> 
    #include <sys/stat.h> 
    #include <fcntl.h>  
    
    main() 
    {  
        int  fd;  
        char *leds = "/dev/leds";  
        char *test1 = "/bin/test1";  
        char *test2 = "/bin/test2";    
        if((fd = open(leds,O_RDWR|O_NOCTTY|O_NDELAY))<0)  
        {   
            printf("open %s failed!\n",leds);  
        }    
        
        printf("\n%s fd is %d\n",leds,fd);    
        
        if((fd = open(test1,O_RDWR,0777))<0)  
        {   
            printf("open %s failed!\n",test1);  
         }    
         
         printf("%s fd is %d\n",test1,fd);    
         
         if((fd = open(test2,O_RDWR|O_CREAT,0777))<0)  
         {   
             printf("open %s failed!\n",test2);  
         }    
         
         printf("%s fd is %d\n",test2,fd); 
       }

    如果/bin/目录下已经存在leds和test1文件,那么三个fd都会返回对应的值,否则只有test2能返回句柄值。

3.创建文件函数 creat

    creat函数用法与open函数相同且完全可以用open函数代替,现在已经很少使用creat函数,下面是其函数原型:

     int creat(const char *path,mode_t mode);

    creat函数只有两个参数,返回值为对应文件句柄,其参数定义可参照open函数:

    • 第一个参数path为绝对路径

    • 第二个参数mode为设置创建文件的权限

      其测试例程这里不再贴出

4.关闭文件函数close

    任何一个文件在操作完成之后都需要关闭,即通过close函数来实现,调用close 函数之后,会取消open 函数建立的映射关系,句柄将不再有效,占用的空间将被系统释放。下面是其函数原型:

     int close(int fd);

    clsoe函数的参数很简单且只有一个,返回值一般很少使用:

    • 参数fd为使用open函数打开对应文件所返回的句柄

      其调用方法较为简单,例程在后续实验中会用到。

5.写入文件函数write

    对已经打开的文件进行写入操作的函数,其函数原型为:

     ssize_t write(int fd ,const void *buf,size_t count);

    write函数有三个参数一个返回值,返回值类型为ssize,出错时返回-1,其他值返回实际写入字节数:

    • 第一个参数fd,是对应文件open操作时返回的句柄

    • 第二个参数*buf,是需要写入的数据

    • 第三个参数count,表示将第二个参数*buf中最多count个字节写入对应文件

      write函数的例程如下:

     #include <stdio.h> 
     #include <unistd.h> 
     #include <sys/types.h> 
     #include <sys/stat.h> 
     #include <fcntl.h> 
     #include <string.h>  
     
     main() 
     {  
         int  fd;  
         char *testwrite = "/bin/testwrite";  
         ssize_t length_w;  
         char buffer_write[] = "Hello Write Function!";    
         
         if((fd = open(testwrite,O_RDWR|O_CREAT,777))<0)  
         {    
             printf("open %s failed \n",testwrite);  
         }
             
         length_w = write(fd,buffer_write,strlen(buffer_write));  
         
         if(length_w == -1)  
         {   
             perror("write");  
         }  
         else  
         {   
             printf("Write Function OK!\n And the length of input is %d",length_w);  
         }  
         close(fd); 
     }

    最终在屏幕上输出

     Write Function OK! And the length of input is 21

6.读取文件函数read

    对文件进行读取操作,使用较为频繁,其函数原型为:

     ssize_t read(int fd,void *buf,size_t len);

    read函数有三个参数一个返回值,返回值类型为ssize,出错时返回-1,其他值返回实际读取字节数:

    • 第一个参数fd,是对应文件open操作时返回的句柄

    • 第二个参数*buf,是读取数据保存的位置

    • 第三个参数len,表示每次从对应文件中读取最多count个字节存入第二个参数指定的位置。

      open函数的例程如下:

     #include <stdio.h> 
     #include <unistd.h> 
     #include <sys/types.h> 
     #include <sys/stat.h> 
     #include <fcntl.h> 
     #include <string.h>  
     #define MAX_SIZE 1000  
     main() 
     {  
         int fd;  
         ssize_t length_w,ret,length_r = MAX_SIZE;  
         char *testwrite = "/bin/testwrite";  
         char buffer_write[] = "Hello Write Function!";  
         char buffer_read[MAX_SIZE];    
         if((fd = open(testwrite,O_RDWR|O_CREAT,777))<0)  
         {   
             printf("open %s failed!\n",testwrite);  
         }  
         
         length_w = write(fd,buffer_write,strlen(buffer_write));  
         
         if(length_w == -1)  
         {   
             perror("write");  
         }  
         else  
         {   
             printf("Write Function OK\n And the length of input is %d !\n",length_w);  
         }  
         
         close(fd);    
         
         if((fd = open(testwrite,O_RDWR|O_CREAT,777))<0)  
         {   
             printf("open %s failed!\n",testwrite);  
         }  
         
         ret = read(fd,buffer_read,length_r);  
         
         if(ret == -1)  
         {   
             perror("read");  
         }  
         else  
         {   
             printf("Files Content is %s \n And the length of read is %d !\n",buffer_read,ret);  
         }  
         
         close(fd); 
     }

最终在屏幕上输出

 Write Function OK And the length of input is 21 ! Files Content is Hello Write Function!  And the length of read is 21 !

7.结束语

花了两个晚上才完成文件IO操作的学习,都是很基础的知识,以后用的也比较多,所以虽然比较简单但还是跟着教程一步一步走了一遍,希望能加深印象,共勉~