文件操作函数(2)

时间:2023-01-25 09:57:40

文件的打开读写操作



打开文件函数fopen

(1)   函数原型:FILE * fopen(const char * path,const char * mode)
(2)   返回值:文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回NULL,并把错误代码存在errno中。


(3)   path:  操作文件路径
(4)   mode:  打开模式


mode模式如下:
"r"           以只读方式打开文件,该文件必须存在
"r+"        以可读写方式打开文件,该文件必须存在。
"rb+"      读写打开一个二进制文件,允许读写数据,文件必须存在。

"w"         打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。
"w+"       打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。

"a"          以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。(EOF符保留)
"a+"       以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 (原来的EOF符不保留)

"wb"       只写打开或新建一个二进制文件;只允许写数据。
"wb+"     读写打开或建立一个二进制文件,允许读和写
"wx"        创建文本文件,只允许写入数据.[C11]
"wbx"      创建一个二进制文件,只允许写入数据.[C11]
"w+x"      创建一个文本文件,允许读写.[C11]
"wb+x"    创建一个二进制文件,允许读写.[C11]
"w+bx"    和"wb+x"相同[C11]

"rt"           只读打开一个文本文件,只允许读数据
"wt"         只写打开或建立一个文本文件,只允许写数据
"at"          追加打开一个文本文件,并在文件末尾写数据
"rb"          只读打开一个二进制文件,只允许读数据
"wb"        只写打开或建立一个二进制文件,只允许写数据
"ab"         追加打开一个二进制文件,并在文件末尾写数据
"rt+"         读写打开一个文本文件,允许读和写
"wt+"       读写打开或建立一个文本文件,允许读写
"at+"        读写打开一个文本文件,允许读,或在文件末追加数据
"rb+"        读写打开一个二进制文件,允许读和写
"ab+"       读写打开一个二进制文件,允许读,或在文件末追加数据


以x结尾的模式为独占模式,文件已存在或者无法创建(一般是路径不正确)都会导致fopen失败.
文件以操作系统支持的独占模式打开.[C11]

上述的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,加入b 字符用来告诉函数库以二进制模式打开文件。
如果不加b,表示默认加了t,即rt,wt,其中t表示以文本模式打开文件。
由fopen()所建立的新文件会具有S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH(0666)权限,此文件权限也会参考umask值。
有些C编译系统可能不完全提供所有这些功能,有的C版本不用"r+","w+","a+",而用"rw","wr","ar"等,读者注意所用系统的规定。


(5)   二进制和文本模式的区别
1)   在windows系统中,文本模式下,文件以"\r\n"代表换行。
若以文本模式打开文件,并用fputs等函数写入换行符"\n"时,函数会自动在"\n"前面加上"\r"。
即实际写入文件的是"\r\n" 。
2)   在类Unix/Linux系统中文本模式下,文件以"\n"代表换行。
所以Linux系统中在文本模式和二进制模式下并无区别。


(6)   打开方式总结:各种打开方式主要有三个方面的区别:
①打开是否为二进制文件,用“b”标识。
②读写的方式,有以下几种:只读、只写、读写、追加只写、追加读写这几种方式。
③对文件是否必 须存在、以及存在时是清空还是追加会有不同的响应。具体判断如下图。


文件操作函数(2)







文件读函数 fread

功能:  fread是一个函数。从一个文件流中读数据,最多读取count个项,每个项size个字节,如果调用成功返回实际读取到的项个数(小于或等于count),如果不成功或读到文件末尾返回 0。

函数原型:   size_t fread ( void *buffer, size_t size, size_t count, FILE *stream) 

(1)   buffer:   用于接收数据的内存地址
(2)   size:   要读的每个数据项的字节数,单位是字节
(3)   count:   要读count个数据项,每个数据项size个字节
(4)   stream:   输入流
(5)   返回值:   返回真实写入的项数,若大于count则意味着产生了错误。


另外,产生错误后,文件位置指示器是无法确定的。
若其他stream或buffer为空指针,或在unicode模式中写入的字节数为奇数,
此函数设置errno为EINVAL以及返回0.


文件写函数 fwrite

功能:fwrite是C语言函数,指向文件写入一个数据块。
函数原型:   size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
(1)   buffer:   是一个指针,对fwrite来说,是要获取数据的地址;
(2)   size:   要写入内容的单字节数;
(3)   count:   要进行写入size字节的数据项的个数;
(4)   stream:   目标文件指针;
(5)   返回实际写入的数据项个数count。


说明:写入到文件的哪里? 这个与文件的打开模式有关,如果是w+,则是从file pointer指向的地址开始写,
替换掉之后的内容,文件的长度可以不变,stream的位置移动count个数;
如果是a+,则从文件的末尾开始添加,文件长度加大。
fseek对此函数有作用,但是fwrite  函数写到用户空间缓冲区,并未同步到文件中,
所以修改后要将内存与文件同步可以用fflush(FILE *fp)函数同步。


示例1:

#include <stdio.h>
#include <errno.h>

#define DPRINT printf
#define EPRINT printf

#define FILE_PATH "/usr/test"

int main(void)
{
FILE* fp=NULL;
fp=fopen(FILE_PATH,"r");
if(NULL == fp)
{
EPRINT("open file error(%d)", errno);
return -1;
}
fclose(fp);
fp = NULL;

return 0;
}


示例二:


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

#define DPRINT printf
#define EPRINT printf

#define FILE_PATH "/usr/test"

int main(void)
{
FILE* fp;
char msg[16] = "this is a test";
char buf[20] = {0};
fp = fopen(FILE_PATH, "a+");

if(NULL == fp)
{
EPRINT("open file error(%d)", errno);
return -1;
}

fwrite(msg, 1, strlen(msg)+1, fp);

fseek(fp, 0, SEEK_SET);

fread(buf, 1, strlen(msg)+1, fp);
DPRINT("%s\n", buf);

fclose(fp);

return 0;
}




ERRNO错误对照表:

errno0 :     Success

errno1 :     Operation not permitted

errno2 :     No such file or directory

errno3 :     No such process

errno4 :     Interrupted system call

errno5 :     Input/output error

errno6 :     No such device or address

errno7 :     Argument list too long

errno8 :     Exec format error

errno9 :     Bad file descriptor

errno10 :    No child processes

errno11 :    Resource temporarily unavailable

errno12 :    Cannot allocate memory

errno13 :    Permission denied

errno14 :    Bad address

errno15 :    Block device required

errno16 :    Device or resource busy

errno17 :    File exists

errno18 :    Invalid cross-device link

errno19 :    No such device

errno20 :    Not a directory

errno21 :    Is a directory

errno22 :    Invalid argument

errno23 :    Too many open files in system

errno24 :    Too many open files

errno25 :    Inappropriate ioctl for device

errno26 :    Text file busy

errno27 :    File too large

errno28 :    No space left on device

errno29 :    Illegal seek

errno30 :    Read-only file system

errno31 :    Too many links

errno32 :    Broken pipe

errno33 :    Numerical argument out of domain

errno34 :    Numerical result out of range

errno35 :    Resource deadlock avoided

errno36 :    File name too long

errno37 :    No locks available

errno38 :    Function not implemented

errno39 :    Directory not empty

errno40 :    Too many levels of symbolic links

errno41 :    Unknown error 41

errno42 :    No message of desired type

errno43 :    Identifier removed

errno44 :    Channel number out of range

errno45 :    Level 2 not synchronized

errno46 :    Level 3 halted

errno47 :    Level 3 reset

errno48 :    Link number out of range

errno49 :    Protocol driver not attached

errno50 :    No CSI structure available

errno51 :    Level 2 halted

errno52 :    Invalid exchange

errno53 :    Invalid request descriptor

errno54 :    Exchange full

errno55 :    No anode

errno56 :    Invalid request code

errno57 :    Invalid slot

errno58 :    Unknown error 58

errno59 :    Bad font file format

errno60 :    Device not a stream

errno61 :    No data available

errno62 :    Timer expired

errno63 :    Out of streams resources

errno64 :    Machine is not on the network

errno65 :    Package not installed

errno66 :    Object is remote

errno67 :    Link has been severed

errno68 :    Advertise error

errno69 :    Srmount error

errno70 :    Communication error on send

errno71 :    Protocol error

errno72 :    Multihop attempted

errno73 :    RFS specific error

errno74 :    Bad message

errno75 :    Value too large for defined datatype

errno76 :    Name not unique on network

errno77 :    File descriptor in bad state

errno78 :    Remote address changed

errno79 :    Can not access a needed sharedlibrary

errno80 :    Accessing a corrupted sharedlibrary

errno81 :    .lib section in a.out corrupted

errno82 :    Attempting to link in too manyshared libraries

errno83 :    Cannot exec a shared librarydirectly

errno84 :    Invalid or incomplete multibyte orwide character

errno85 :    Interrupted system call should berestarted

errno86 :    Streams pipe error

errno87 :    Too many users

errno88 :    Socket operation on non-socket

errno89 :    Destinationaddress required

errno90 :    Message too long

errno91 :    Protocol wrong type for socket

errno92 :    Protocol not available

errno93 :    Protocol not supported

errno94 :    Socket type not supported

errno95 :    Operation not supported

errno96 :    Protocol family not supported

errno97 :    Address family not supported byprotocol

errno98 :    Address already in use

errno99 :    Cannot assign requested address

errno100 :   Network is down

errno101 :   Network is unreachable

errno102 :   Network dropped connection onreset

errno103 :   Software caused connection abort

errno104 :   Connection reset by peer

errno105 :   No buffer space available

errno106 :   Transport endpoint is alreadyconnected

errno107 :   Transport endpoint is notconnected

errno108 :   Cannot send after transportendpoint shutdown

errno109 :   Too many references: cannot splice

errno110 :   Connection timed out

errno111 :   Connection refused

errno112 :   Host is down

errno113 :   No route to host

errno114 :   Operation already in progress

errno115 :   Operation now in progress

errno116 :   Stale NFS file handle

errno117 :   Structure needs cleaning

errno118 :   Not a XENIX named type file

errno119 :   No XENIX semaphores available

errno120 :   Is a named type file

errno121 :   Remote I/O error

errno122 :   Disk quota exceeded

errno123 :   No medium found

errno124 :   Wrong medium type

errno125 :   Operation canceled

errno126 :   Required key not available

errno127 :   Key has expired

errno128 :   Key has been revoked

errno129 :   Key was rejected by service

errno130 :   Owner died

errno131 :   State not recoverable

errno132 :   Operation not possible due toRF-kill

errno133 :   Unknown error 133

errno134 :   Unknown error 134

errno135 :   Unknown error 135

errno136 :   Unknown error 136

errno137 :   Unknown error 137

errno138 :   Unknown error 138

errno139 :   Unknown error 139




转自: 

http://baike.baidu.com/link?url=2zIYaX86gIqf_yo2KBUgI-Wk46OaGqSC2Jd3IAH5ZcYnO6TgKpLBNgman4Usqt5TZgNY7dE08hGj8vwzSEqh7q