`
xitonga
  • 浏览: 589690 次
文章分类
社区版块
存档分类
最新评论

文件I/O(1)

 
阅读更多

文件I/O(1)

文件描述符

对内核而言,所以打开的文件都通过文件描述符引用。每个进程都有一些与之关联的文件描述符。文件描述符是一个非负整数。当打开一个现有文件或创建一个新文件时,内核向进程返回一个文件描述符。当读或写一个文件时,使用open或creat返回的文件描述符标识该文件,将其作为参数传送给read和write。

一般有三个以及打开的文件描述符,他们是:

0:标准输入 STDIN_FILENO

1:标准输出 STDOUT_FILENO

2标准错误输出 STDERR_FILENO

每行后面的符号常量是依从POSIX而定的。

open函数

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

int open(const char *pathname, int flags);

int open(const char *pathname, int flags,mode_t mode);

pathname是要打开或创建文件的名字。

flag用来定义打开文件所采取的的动作,必须调用以下模式之一

O_RDONLY,O_WRONLY, O_RDWR分别代表只读,只写,读写方式打开。

open还可以包括以下可选模式的组合

O_APPEND:把写入数据追加到文件的尾端

O_CREAT:若文件不存在,则创建它。使用此选项时,需要第三个参数mode,用其指定该新文件的访问权限。

O_EXCL:如果同时指定了O_CREAT,而文件存在,则会出错。用此可以测试一个文件是否存在,如果存在,则创建文件,这使测试和创建两者成为一个原子操作。

O_TRUNC: 如果此文件存在,而且为只写或读写成功打开,则将其长度截为0。

open返回的文件描述符一定是最小的未用描述符数值。这一点被某些应用程序用在标准输入,标准输出或标准错误输出上。如,一个程序关闭了自己的标准输出,然后再次调用open,文件描述符1就会被调用,并且标准输出将被有效的重定向到另一个文件或设备。

POSIX规范还标准化了一个creat调用,此函数等效于

open(pathname,O_WONLY |O_CREAT | O_TRUNC, mode);

close函数

#include <unistd.h>

int close(int fd);

close调用终止一个文件描述符fd与对应文件之间的关联。文件描述符被释放后并能重新使用。close调用成功返回0,出错返回-1.

关闭一个文件时会释放该进程加在文件上的所有记录锁。当一个进程终止时,内核自动关闭它所有打开的文件。

lseek函数

每个打开的文件都有一个与其相关联的”当前文件偏移量”。按系统默认情况,当打开一个文件时,除非指定O_APPEND选项,否则该偏移量被设置为0。lseek可以为一个打开的文件设置偏移量。

#include <sys/types.h>

#include <unistd.h>

off_t lseek(int fd, off_t offset, intwhence);

offset用来指定位置,whence参数定义该偏移值的用法。whence可取以下值:

SEEK_SET: The offset is set to offset bytes.

SEEK_CUR: The offset is set to its current locationplus offset bytes.

SEEK_END: The offset is set to the size of the fileplus offset bytes.

成功调用返回从文件头到文件指针被设置处的字节偏移值,失败返回-1。参数offset定义在<sys/types.h>中。

当偏移量大于文件长度时,出现空洞,空洞不占用存储区。

read函数

#include <unistd.h>

ssize_t read(int fd, void *buf, size_tcount);

将与文件描述符fd关联的文件中读入count个字符放到buf中。返回读入的字节数,它可能小于请求的字节数。如果read调用返回0,就表示未读入任何数据,已到达了文件尾。返回-1,就表示出错。

write函数

#include <unistd.h>

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

把缓冲区buf的前count个字节写入与文件描述符fd相关联的文件中。返回实际写入的字节数,通常与count值相同;否则表示出错。出错的一个常见原因是:磁盘已写满,或者超出了一个给定进程的文件长度限制。

实例:创建一个文件,写入数据,移动当前偏移量,在读数据。

#include<unistd.h> //<unistd.h>必须最早出现,因为它可能会影响到其他头文件。#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include<errno.h>
int main()
{
   char* filename = ".//file";
   char buf[100];
   char buf1[5];
   int fd;
 
   printf("open a file to write\n");
   if((fd = open(filename,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH ))==-1)
    {
       perror("cannot open file\n");
       return 1;
    }
   printf("open file successfully!\n");
   printf("input a string:");
gets(buf);
//write intofile
    if(write(fd,buf,strlen(buf)) !=strlen(buf))
    {
        perror("cannot write intofile\n");
        return 1;
    }
    close(fd);
 
    printf("open file to read.\n");
    if((fd=open(filename,O_RDONLY)) == -1)
    {
        perror("cannot open thefile.\n");
        return 1;
    }
    if(lseek(fd,3,SEEK_SET) == -1)
    {
        perror("lseek erroe\n");
        return 1;
    }
    //read from the file
    if(read(fd,buf1,4)==-1)
    {
perror("read error.\n");
        return 1;
    }
    printf("read from file is%s\n",buf1);
    close(fd);
 
    return 0;
}


执行与输出结果:

chen123@ubuntu:~$gcc -o io io.c

chen123@ubuntu:~$./io

open a file towrite

open filesuccessfully!

input a string:akxivbaslzkncxcasbxbwwvaidxbd

open file toread.

read from fileis ivba

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics