C++文件操作

来源:互联网 发布:上海市人工智能协会 编辑:程序博客网 时间:2024/05/16 17:32

C语言上是C++的子集,所以C++操作文件可以使用C语言的函数。C++的fstream类提供了对文件的操作,但是不常用,C++操作文件的时候用C语言的函数还是方便一些。

C语言使用FILE结构体操作文件,使用FILE结构体时,需要包含头文件

#include<cstdio>

下面的代码是一个示例

#include<iostream>#include<fstream>#include<cstdio>using namespace std;int main(){FILE *f=fopen("D:\\d.txt","wb");fwrite("zhy_cheng,我是攻城师",1,sizeof("zhy_cheng,我是攻城师"),f);fclose(f);return 0;}

fopen函数返回一个指向FILE结构体的指针,如果打开错误,返回NULL。fopen函数的原型如下

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

第一个参数是打开的文件名,第二个参数是打开的模式,介绍如下:

The character string mode specifies the type of access requested for the file, as follows:

"r"

Opens for reading. If the file does not exist or cannot be found, thefopen call fails.

"w"

Opens an empty file for writing. If the given file exists, its contents are destroyed.

"a"

Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t exist.

"r+"

Opens for both reading and writing. (The file must exist.)

"w+"

Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.

"a+"

Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.

When a file is opened with the "a" or"a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned usingfseek or rewind, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.

The "a" mode does not remove the EOF marker before appending to the file. After appending has occurred, the MS-DOS TYPE command only shows data up to the original EOF marker and not any data appended to the file. The "a+" mode does remove the EOF marker before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file. The"a+" mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker.

When the "r+", "w+", or"a+" access type is specified, both reading and writing are allowed (the file is said to be open for “update”). However, when you switch between reading and writing, there must be an interveningfflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.

In addition to the above values, the following characters can be included inmode to specify the translation mode for newline characters:

t

Open in text (translated) mode. In this mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing with"a+", fopen checks for a CTRL+Z at the end of the file and removes it, if possible. This is done because usingfseek and ftell to move within a file that ends with a CTRL+Z, may causefseek to behave improperly near the end of the file.

Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output. When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters (as if by a call to thembtowc function). For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to thewctomb function).

b

Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed.

If t or b is not given inmode, the default translation mode is defined by the global variable _fmode. Ift or b is prefixed to the argument, the function fails and returnsNULL.

 

c

Enable the commit flag for the associatedfilename so that the contents of the file buffer are written directly to disk if eitherfflush or _flushall is called.

n

Reset the commit flag for the associatedfilename to “no-commit.” This is the default. It also overrides the global commit flag if you link your program with COMMODE.OBJ. The global commit flag default is “no-commit” unless you explicitly link your program with COMMODE.OBJ.

 

fwrite函数的原型如下:

size_t fwrite( const void *buffer, size_tsize, size_t count, FILE *stream);

返回实际写入的字节数,如果比count小,则写入是发生了错误,那么文件指针的位置就不能知道在哪里了。

第一个参数是写入数据的地址,

第二个参数是一次写入多少个字节,

打三个参数是写多少次,

第四个参数是使用的FILE结构体的指针。

例如:要写入一个int类型,那么可以一次写入4个字节,那么写一次就够了,如果一次写一个字节,需要写4次。

fseek是移动读写指针用的,函数原型如下:

int fseek( FILE *stream, longoffset, int origin );

第一个参数是FIEL结构体指针

第二个参数是偏移值

第三个参数是原始值,可以取值为:

SEEK_CUR

Current position of file pointer

SEEK_END

End of file

SEEK_SET

Beginning of file

如果成功,返回0,失败返回非0值。

fread函数与fwrite函数及其相似,这里不说了。

ftell函数可以获得文件指针的位置

我可以先将指针移动文件末尾,再使用ftell函数,这样就可以获得文件的长度了,然后根据文件的长度为文件分配内存。

rewind函数重新放置文件指针放到文件开始处。