naotang的测试成长空间,记录工作中的问题,学习中的心得。 个人网站:www.naotang.com

C 中关于文件操作的函数

上一篇 / 下一篇  2007-09-14 14:22:21 / 个人分类:其他资源

// fopen()函数51Testing软件测试网ai9Ly3h*j
FILE *fopen(const char *filename, const char *mode)51Testing软件测试网|g3c!dYvB
fopen opens the named file, and returns a stream, or NULL if the attempt fails. Legal values for mode include:
(b+R wh;B+O0"r" open text file for reading
!oD X8x Dtd4d0"w" create text file for writing; discard previous contents if any51Testing软件测试网oNa)a1_`} `M7F
"a" append; open or create text file for writing at end of file51Testing软件测试网;Ua-E q/_6D7B)l9Q7A-X
"r+" open text file for update (i.e., reading and writing)
&WTh^ D-~/@ |a+N0"w+" create text file for update, discard previous contents if any
"w.H0nay4w1A0"a+" append; open or create text file for update, writing at end
p q$F.N,^0W051Testing软件测试网)E&]0T(I4Z;qr"x P
Update mode permits reading and writing the same file; fflush or a file-positioning function must be called between a read and a write or vice versa. If the mode includes b after the initial letter, as in "rb" or "w+b", that indicates a binary file. Filenames are limited to FILENAME_MAX characters. At most FOPEN_MAX files may be open at once.
[)TCx^pMG+S2L)C%]051Testing软件测试网 j v+g9KL)X'F
// freopen()函数51Testing软件测试网6Eu|/u}WR `q
FILE *freopen(const char *filename, const char *mode, FILE *stream)
W!}4k `^FD0freopen opens the file with the specified mode and associates the stream with it. It returns stream, or NULL if an error occurs. freopen is normally used to change the files associated with stdin, stdout, or stderr.
5E tu%RyP0a'`*b!w051Testing软件测试网 FV:nT)N uHV
// fseek()函数,用于各种定位
r/W$Xg7K&D ?0int fseek(FILE *stream, long offset, int origin)51Testing软件测试网m uon7?G\
fseek sets the file position for stream; a subsequent read or write will access data beginning at the new position. For a binary file, the position is set to offset characters from origin, which may be SEEK_SET (beginning), SEEK_CUR (current position), or SEEK_END (end of file). For a text stream, offset must be zero, or a value returned by ftell (in which case origin must be SEEK_SET). fseek returns non-zero on error.51Testing软件测试网mecA&qA

?Me7r$r8x^.hc`0// ftell()函数,用于获得位置(可以用一个变量记下,方便返回)51Testing软件测试网"Z:Yadbf
long ftell(FILE *stream)
Z6v&tS8K(yGQ0ftell returns the current file position for stream, or -1 on error.51Testing软件测试网4r*j2`2U['|2~
51Testing软件测试网Ycp J.[GO
// 定位至文件头,这个可能对修改文件最有用
Ig3IG#E5wu&M#M O R0void rewind(FILE *stream)
p'InX:}0rewind(fp) is equivalent to fseek(fp, 0L, SEEK_SET); clearerr(fp).
/j2t;CEO,Q V4R8}"b051Testing软件测试网bW2g'hq%}W.N9d
// 获得位置,注意这个主要用于二进制文件51Testing软件测试网cj;X!i mPA2lUz
int fgetpos(FILE *stream, fpos_t *ptr)51Testing软件测试网G#J"a#V.XIL(M
fgetpos records the current position in stream in *ptr, for subsequent use by fsetpos. The type fpos_t is suitable for recording such values. fgetpos returns non-zero on error.51Testing软件测试网l q#j`%_ ]

3D0Z'tQ/c.P0// 位置跳转,用于二进制文件
!~ FT(M4L!n5X%d0int fsetpos(FILE *stream, const fpos_t *ptr)51Testing软件测试网;O}1Km2v,S@
fsetpos positions stream at the position recorded by fgetpos in *ptr. fsetpos returns non-zero on error.

TAG: fopen fprintf 其他资源

 

评分:0

我来说两句

Open Toolbar