标准IO与文件IO

文件:一组相关数据的有序集合;

文件名:这个数据集合的名称。


一、标准IO:标准I/O是ANSI C建立的一个标准I/O模型,是一个标准函数包和stdio.h头文件中的定义,具有一定的可移植性。标准IO库处理很多细节。例如缓存分配,以优化长度执行IO等。标准的IO提供了三种类型的缓存。

常用操作:

fopen/fclose;

fwrite/fread;

fgets/fputs;

fgetc/fputc;


1.1:fopen();

 #include <stdio.h>

   FILE *fopen(const char *path, const char *mode);

 The fopen() function opens the file whose name is the string pointed to by path and associates a stream with it.


       The argument mode points to a string beginning with  one  of  the  following  sequences (Additional characters may follow these sequences.):

       r      Open  text  file  for reading.  The stream is positioned at the beginning of the file.

       r+     Open for reading and writing.  The stream is positioned at the beginning of  the file.

       w      Truncate  file  to  zero  length or create text file for writing.  The stream is positioned at the beginning of the file.

       w+     Open for reading and writing.  The file is created if it does not exist,  other‐wise it is truncated.  The stream is positioned at the beginning of the file.

       a      Open for appending (writing at end of file).  The file is created if it does not exist.  The stream is positioned at the end of the file.

       a+     Open for reading and appending (writing at end of file).  The file is created if it does not exist.  The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

1.2: fclose();

 #include <stdio.h>

       int fclose(FILE *fp);

1.3:fread();

 #include <stdio.h>

       size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

1.4:fwrite();

 #include <stdio.h>

       size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);


       size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

RETURN VALUE
       fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters).  If an error occurs, or  the  end-of-file  is  reached,  the return value is a short item count (or zero).

2.标准IO缓存分类:

全缓存:当填满标准IO缓存后才进行实际的IO操作。 
行缓存:当输入或输出中遇到新行符(‘\n’)时,标准IO库执行IO操作。 
不带缓存:stderr。

标准IO预定3个流,他们可以自动为进程使用。

stdin:0

stdout:1

stderr:2


二、文件IO:文件IO称之为不带缓存的IO(unbuffered I/O)。不带缓存指的是每个read,write都调用内核中的一个系统调用。也就是一般所说的低级I/O——操作系统提供的基本IO服务,与os绑定,特定于linix或unix平台。

常用操作:

open/close;

read/write;

lseek;

2.1: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);
       int creat(const char *pathname, mode_t mode);


2.2:close();

  #include <unistd.h>


       int close(int fd);


2.3:read();

 #include <unistd.h>

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


2.4:write();

  #include <unistd.h>

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


RETURN VALUE
       On  success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number.  It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are  reading  from  a pipe, or from a terminal), or because read() was interrupted by a signal.  On error, -1 is returned, and errno is set appropriately.  In this case it  is left unspecified whether the file position (if any) changes.


2.5:fseek();

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

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


 The  lseek()  function repositions the offset of the open file associated with the file descriptor fd to the argument offset according to the directive whence as follows:

       SEEK_SET
              The offset is set to offset bytes.

       SEEK_CUR
              The offset is set to its current location plus offset bytes.

       SEEK_END
              The offset is set to the size of the file plus offset bytes.

从虚拟机中复制ctrl+shift+c,粘贴到虚拟机ctrl+shift+v;


三、标准IO与文件IO区别(此处内容来源网络):

首先:两者一个显著的不同点在于,标准I/O默认采用了缓冲机制,比如调用fopen函数,不仅打开一个文件,而且建立了一个缓冲区(读写模式下将建立两个缓冲区),还创建了一个包含文件和缓冲区相关数据的数据结构。低级I/O一般没有采用缓冲,需要自己创建缓冲区,不过其实在linix或unix系统中,都是有使用称为内核缓冲的技术用于提高效率,读写调用是在内核缓冲区和进程缓冲区之间进行的数据复制。其次从操作的设备上来区分,文件I/O主要针对文件操作,读写硬盘等,它操作的是文件描述符,标准I/O针对的是控制台,打印输出到屏幕等,它操作的是字符流。对于不同设备得特性不一样,必须有不同api访问才最高效。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值