文件操作fopen, fclose, fread, fwrite, fseek, ftell
2015-04-05 22:46
676 查看
fopen
FILE * fopen ( const char * filename, const char * mode );
fclose
int fclose ( FILE * stream );
fread
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
Read block of data from stream
Reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by ptr.
The position indicator of the stream is advanced by the total amount of bytes read.
The total amount of bytes read if successful is (size*count).
fwrite
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
Write block of data to stream
Writes an array of count elements, each one with a size of size bytes, from the block of memory pointed by ptr to the current position in the stream.
The position indicator of the stream is advanced by the total number of bytes written.
Internally, the function interprets the block pointed by ptr as if it was an array of
(size*count)elements of type
unsigned char, and writes them sequentially to stream as if fputc was
called for each byte.
fseek
int fseek ( FILE * stream, long int offset, int origin );
Reposition stream position indicator
Sets the position indicator associated with the stream to a new position.
For streams open in binary mode, the new position is defined by adding offset to a reference position specified byorigin.
For streams open in text mode, offset shall either be zero or a value returned by a previous call to ftell, and origin shall
necessarily be SEEK_SET.
If the function is called with other values for these arguments, support depends on the particular system and library implementation (non-portable).
The end-of-file internal indicator of the stream is cleared after a successful call to this function, and all effects from previous calls to ungetc on
this stream are dropped.
On streams open for update (read+write), a call to fseek allows to switch between reading and writing.
Parameters
streamPointer to a FILE object that identifies the stream.offsetBinary files: Number of bytes to offset from origin.
Text files: Either zero, or a value returned by ftell.
originPosition used as reference for the offset. It is specified by one of the following constants defined in <cstdio>exclusively
to be used as arguments for this function:
Constant | Reference position |
---|---|
SEEK_SET | Beginning of file |
SEEK_CUR | Current position of the file pointer |
SEEK_END | End of file * |
ftell
long int ftell ( FILE * stream );
Get current position in stream
Returns the current value of the position indicator of the stream.
For binary streams, this is the number of bytes from the beginning of the file.
For text streams, the numerical value may not be meaningful but can still be used to restore the position to the same position later using fseek (if there
are characters put back using ungetc still pending of being read, the behavior is undefined).
参考资料
http://www.cplusplus.com/相关文章推荐
- c文件操作之fopen、fclose、fread、fwrite及相关fseek、ftell、rewind例子
- Linux下文件的相关操作(fopen、fwrite、fread、fseek、ftell)
- 一个文件操作的简单程序:关于文件的fopen、fread、fseek、ftell、fclose等操作(FIFO related)
- c语言中所有文件操作函数详解fopen、fwrite、fread、fgetc、fputc、fscanf、fprintf、ftell、fseek等函数
- 一个文件操作的简单程序:关于文件的fopen、fread、fseek、ftell、fclose等操作(FIFO related)
- C文件操作——fopen/fseek/ftell/fread/fwrite/fclose等函数用法
- c语言中所有文件操作函数详解fopen、fwrite、fread、fgetc、fputc、fscanf、fprintf、ftell、fseek等函数
- c语言中所有文件操作函数详解fopen、fwrite、fread、fgetc、fputc、fscanf、fprintf、ftell、fseek等函数
- 【matlab】matlab文件操作fopen,fseek,fread,fclose等
- fopen、fread、fwrite、fclose、fseek、feof、ftell的用法简介
- matlab文件操作fopen-fseek-fread-fclose等
- linux c语言之文件操作相关函数--fopen(),fopen_s(),fread(),fwrite(),fclose(),
- matlab文件操作及读txt文件(fopen,fseek,fread,fclose)
- 文件内容操作篇clearerr fclose fdopen feof fflush fgetc fgets fileno fopen fputc fputs fread freopen fseek ftell fwrite getc getchar gets
- 文件编程中的两套操作:标准C库(fread, fwrite,fclose,fopen...)和 系统调用(open, read, write...)
- 15:文件操作(C标准库函数)fopen/fclose/fread/fwrite/fseek/fprintf/fscanf
- 文件内容操作篇clearerr fclose fdopen feof fflush fgetc fgets fileno fopen fputc fputs fread freopen fseek ftell fwrite getc getchar gets
- fopen、fclose、feof、fputc、fgetc、fread、fwrite、ftell、fseek、fscanf函数的用法
- linux编程之文件操作fseek,fwrite,fread,ftell使用
- MATLAB文件操作及读txt文件(fopen,fseek,fread,fclose)