C++文件操作

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, the fopen 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 using fseek 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 intervening fflush, 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 in mode 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 using fseek and ftell to move within a file that ends with a CTRL+Z, may cause fseek 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 the mbtowc function). For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to the wctomb function).

b

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

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

 

c

Enable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if either fflush or _flushall is called.

n

Reset the commit flag for the associated filename 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_t size, size_t count, FILE *stream );

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

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

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

打三个参数是写多少次,

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

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

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

int fseek( FILE *stream, long offset, 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函数重新放置文件指针放到文件开始处。

 

 

 

 


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值