C++文件相关操作
来源:互联网 发布:mac ps破解版安装教程 编辑:程序博客网 时间:2024/06/11 22:03
整理了一下系统中与文件操作相关的函数及数据结构
fopen,fclose,fseek,fread,fwrite
FILE,size_t
fopen
function
<cstdio>
FILE * fopen ( const char * filename, const char * mode );
Open file
Opens the file whose name is specified in the parameter filename and associates it with a stream that can be identified in future operations by the FILE object whose pointer is returned. The operations that are allowed on the stream and how these are performed are defined by the mode parameter.
The running environment supports at least FOPEN_MAX files open simultaneously; FOPEN_MAX is a macro constant defined in <cstdio>.
With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
Additional characters may follow the sequence, although they should have no effect. For example, "t" is sometimes appended to make explicit the file is a text file.
In the case of text files, depending on the environment where the application runs, some special character conversion may occur in input/output operations to adapt them to a system-specific text file format. In many environments, such as most UNIX-based systems, it makes no difference to open a file as a text file or a binary file; Both are treated exactly the same way, but differentiation is recommended for a better portability.
For the modes where both read and writing (or appending) are allowed (those which include a "+" sign), the stream should be flushed (fflush) or repositioned (fseek, fsetpos, rewind) between either a reading operation followed by a writing operation or a writing operation followed by a reading operation.
The running environment supports at least FOPEN_MAX files open simultaneously; FOPEN_MAX is a macro constant defined in <cstdio>.
Parameters
- filename
- C string containing the name of the file to be opened. This paramenter must follow the file name specifications of the running environment and can include a path if the system supports it.
- mode
- C string containing a file access modes. It can be:
"r"Open a file for reading. The file must exist."w"Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "a"Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist."r+"Open a file for update both reading and writing. The file must exist."w+"Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file."a+"Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.
With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
Additional characters may follow the sequence, although they should have no effect. For example, "t" is sometimes appended to make explicit the file is a text file.
In the case of text files, depending on the environment where the application runs, some special character conversion may occur in input/output operations to adapt them to a system-specific text file format. In many environments, such as most UNIX-based systems, it makes no difference to open a file as a text file or a binary file; Both are treated exactly the same way, but differentiation is recommended for a better portability.
For the modes where both read and writing (or appending) are allowed (those which include a "+" sign), the stream should be flushed (fflush) or repositioned (fseek, fsetpos, rewind) between either a reading operation followed by a writing operation or a writing operation followed by a reading operation.
Return Value
If the file has been succesfully opened the function will return a pointer to a FILE object that is used to identify the stream on all further operations involving it. Otherwise, a null pointer is returned.fclose
function
<cstdio>
int fclose ( FILE * stream );
Close file
Closes the file associated with the stream and disassociates it.
All internal buffers associated with the stream are flushed: the content of any unwritten buffer is written and the content of any unread buffer is discarded.
Even if the call fails, the stream passed as parameter will no longer be associated with the file.
On failure, EOF is returned.
All internal buffers associated with the stream are flushed: the content of any unwritten buffer is written and the content of any unread buffer is discarded.
Even if the call fails, the stream passed as parameter will no longer be associated with the file.
Parameters
- stream
- Pointer to a FILE object that specifies the stream to be closed.
Return Value
If the stream is successfully closed, a zero value is returned.On failure, EOF is returned.
fseek
function
<cstdio>
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 defined by adding offset to a reference position specified by origin.
The End-of-File internal indicator of the stream is cleared after a call to this function, and all effects from previous calls to ungetc are dropped.
When using fseek on text files with offset values other than zero or values retrieved with ftell, bear in mind that on some platforms some format transformations occur with text files which can lead to unexpected repositioning.
On streams open for update (read+write), a call to fseek allows to switch between reading and writing.
Otherwise, it returns nonzero value.
The End-of-File internal indicator of the stream is cleared after a call to this function, and all effects from previous calls to ungetc are dropped.
When using fseek on text files with offset values other than zero or values retrieved with ftell, bear in mind that on some platforms some format transformations occur with text files which can lead to unexpected repositioning.
On streams open for update (read+write), a call to fseek allows to switch between reading and writing.
Parameters
- stream
- Pointer to a FILE object that identifies the stream.
- offset
- Number of bytes to offset from origin.
- origin
- Position from where offset is added. It is specified by one of the following constants defined in <cstdio>:
SEEK_SETBeginning of fileSEEK_CURCurrent position of the file pointerSEEK_ENDEnd of file
Return Value
If successful, the function returns a zero value.Otherwise, it returns nonzero value.
fread
function
<cstdio>
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 postion indicator of the stream is advanced by the total amount of bytes read.
The total amount of bytes read if successful is (size * count).
If this number differs from the count parameter, either an error occured or the End Of File was reached.
You can use either ferror or feof to check whether an error happened or the End-of-File was reached.
The postion indicator of the stream is advanced by the total amount of bytes read.
The total amount of bytes read if successful is (size * count).
Parameters
- ptr
- Pointer to a block of memory with a minimum size of (size*count) bytes.
- size
- Size in bytes of each element to be read.
- count
- Number of elements, each one with a size of size bytes.
- stream
- Pointer to a FILE object that specifies an input stream.
Return Value
The total number of elements successfully read is returned as a size_t object, which is an integral data type.If this number differs from the count parameter, either an error occured or the End Of File was reached.
You can use either ferror or feof to check whether an error happened or the End-of-File was reached.
fwrite
function
<cstdio>
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 postion indicator of the stream is advanced by the total number of bytes written.
The total amount of bytes written is (size * count).
If this number differs from the count parameter, it indicates an error.
The postion indicator of the stream is advanced by the total number of bytes written.
The total amount of bytes written is (size * count).
Parameters
- ptr
- Pointer to the array of elements to be written.
- size
- Size in bytes of each element to be written.
- count
- Number of elements, each one with a size of size bytes.
- stream
- Pointer to a FILE object that specifies an output stream.
Return Value
The total number of elements successfully written is returned as a size_t object, which is an integral data type.If this number differs from the count parameter, it indicates an error.
FILE
type
<cstdio>
Object containing information to control a stream
This type of object identifies a stream and contains the information needed to control it, including a pointer to its buffer, its position indicator and all its state indicators.
FILE objects are usually created by a call to either fopen or tmpfile, which both return a reference to one of these objects.
The content of a FILE object is not meant to be read from outside the functions of the cstdio library; In fact, its main purpose is to be referenced as an argument in all stream-involving functions of this library to identify the stream to be affected.
Its memory allocation is automatically performed by either fopen or tmpfile, and is the responsibility of the library to free the resources once the stream has been closed using fclose or other means.
On inclusion of the cstdio header file, three objects of type FILE * (pointer to FILE)are automatically created. These are associated with the standard input, output and error streams, and can be accessed respectively through the pointers stdin, stdout and stderr.
FILE objects are usually created by a call to either fopen or tmpfile, which both return a reference to one of these objects.
The content of a FILE object is not meant to be read from outside the functions of the cstdio library; In fact, its main purpose is to be referenced as an argument in all stream-involving functions of this library to identify the stream to be affected.
Its memory allocation is automatically performed by either fopen or tmpfile, and is the responsibility of the library to free the resources once the stream has been closed using fclose or other means.
On inclusion of the cstdio header file, three objects of type FILE * (pointer to FILE)are automatically created. These are associated with the standard input, output and error streams, and can be accessed respectively through the pointers stdin, stdout and stderr.
typedef struct {
short level; /* fill/empty level of buffer */
unsigned flags; /* File status flags */
char fd; /* File descriptor */
unsigned char hold; /* Ungetc char if no buffer */
short bsize; /* Buffer size */
unsigned char *buffer; /* Data transfer buffer */
unsigned char *curp; /* Current active pointer */
unsigned istemp; /* Temporary file indicator */
short token; /* Used for validity checking */
} FILE; /* This is the FILE object */
size_t
type
<cstddef>
Unsigned integral type
size_t corresponds to the integral data type returned by the language operator sizeof and is defined in the <cstddef> header file (among others) as an unsigned integral type.
It expresses a size or count in bytes.
It expresses a size or count in bytes.
- 文件相关操作[C#]
- C文件相关操作
- C语言文件相关操作
- C语言 文件操作相关
- linux C文件操作相关
- linux c 文件操作相关
- C语言文件相关操作
- c/c++ 文件相关操作
- C语言文件相关操作
- C文件操作相关函数解析
- C语言的文件操作相关函数
- C语言相关文件操作函数
- c语言文件操作相关函数
- C 文件操作及相关函数
- C语言文件操作的相关(一)
- Linux C 文件操作相关整理
- c语言关于文件的相关操作
- C语言文件操作相关函数
- boost 网络编程
- 让你一生都受用的100句智慧语录
- 遗传算法绘制Firefox图标
- street View on Google Maps comes to Singapore
- Zend Framework学习笔记—View
- C++文件相关操作
- 诺基亚发布Qt4.6 让开发人员开发过程变得轻松
- 笔记本消费的陷阱,选本注意事项,拒绝忽悠!
- 不要迷恋英雄,起码自己不是
- VB.NET普通窗体使用嵌套作用
- 运算符重载
- 09 12 02 Java SE 学习笔记
- 调整
- 在struts中使用ICacheKeyProvider设置图片缓存