📄 subject_19613.htm
字号:
<p>
序号:19613 发表者:Samuel 发表日期:2002-10-30 09:44:20
<br>主题:送分菜鸟的问题,麻烦大家花一分钟!
<br>内容:用ofstream<BR>打开文本文件时并输出文本时,<BR>怎么样才能让在原来内容的结尾继续写而不是将原来的内容清除掉?<BR>谢谢!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:Norton AntivVirus 回复日期:2002-10-30 09:49:41
<br>内容:ofstream file("text.txt",ios::trunc);
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:凯凯 回复日期:2002-10-30 09:50:04
<br>内容:在构造函数的nMode参数中加上ios::app,这样就是追加模式了。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Norton AntivVirus 回复日期:2002-10-30 10:39:16
<br>内容:我靠,这么精彩的回答居然是"勉强接受",太没人性了吧!?<BR><BR>.................................
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Samuel 回复日期:2002-10-30 10:45:24
<br>内容:两位都说对了一半,<BR>应该是:ofstream file("text.txt",ios::app);<BR>而用ofstream根本就没用什么构造函数,就没用什么nMode参数.<BR>叫我怎么送分呢?<BR>还是委屈一下凯凯吧,不过还是照样谢谢你.<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Samuel 回复日期:2002-10-30 10:47:33
<br>内容:NORTON,<BR>明白为什么勉强接受吗?<BR>谢谢你了.
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:凯凯 回复日期:2002-10-30 11:20:09
<br>内容:这不是构造函数是什么?这不是nMode参数是什么?<BR><BR>ofstream::ofstream<BR>ofstream();<BR><BR>ofstream( const char* szName, int nMode = ios::out, int nProt = filebuf::openprot );<BR><BR>ofstream( filedesc fd );<BR><BR>ofstream( filedesc fd, char* pch, int nLength );<BR><BR>Parameters<BR><BR>szName<BR><BR>The name of the file to be opened during construction.<BR><BR>nMode<BR><BR>An integer that contains mode bits defined as ios enumerators that can be combined with the bitwise OR ( | ) operator. The nMode parameter must have one of the following values: <BR><BR>ios::app The function performs a seek to the end of file. When new bytes are written to the file, they are always appended to the end, even if the position is moved with the ostream::seekp function.<BR><BR><BR>ios::ate The function performs a seek to the end of file. When the first new byte is written to the file, it is appended to the end, but when subsequent bytes are written, they are written to the current position.<BR><BR><BR>ios::in If this mode is specified, then the original file (if it exists) will not be truncated.<BR><BR><BR>ios::out The file is opened for output (implied for all ofstream objects).<BR><BR><BR>ios::trunc If the file already exists, its contents are discarded. This mode is implied if ios::out is specified and ios::ate, ios::app, and ios:in are not specified.<BR><BR><BR>ios::nocreate If the file does not already exist, the function fails.<BR><BR><BR>ios::noreplace If the file already exists, the function fails.<BR><BR><BR>ios::binary Opens the file in binary mode (the default is text mode). <BR>nProt<BR><BR>The file protection specification; defaults to the static integer filebuf::openprot that is equivalent to filebuf::sh_compat. The possible nProt values are: <BR><BR>filebuf::sh_compat Compatibility share mode.<BR><BR><BR>filebuf::sh_none Exclusive mode; no sharing.<BR><BR><BR>filebuf::sh_read Read sharing allowed.<BR><BR><BR>filebuf::sh_write Write sharing allowed.<BR>To combine the filebuf::sh_read and filebuf::sh_write modes, use the logical OR ( || ) operator.<BR><BR>fd<BR><BR>A file descriptor as returned by a call to the run-time function _open or _sopen; filedesc is a typedef equivalent to int.<BR><BR>pch<BR><BR>Pointer to a previously allocated reserve area of length nLength. A NULL value (or nLength = 0) indicates that the stream will be unbuffered.<BR><BR>nLength<BR><BR>The length (in bytes) of the reserve area (0 = unbuffered).<BR><BR>Remarks<BR><BR>The four ofstream constructors are:<BR><BR>Constructor Description <BR>ofstream() Constructs an ofstream object without opening a file. <BR>ofstream( const char*, int, int ) Contructs an ofstream object, opening the specified file. <BR>ofstream( filedesc ) Constructs an ofstream object that is attached to an open file. <BR>ofstream( filedesc, char*, int ) Constructs an ofstream object that is associated with a filebuf object. The filebuf object is attached to an open file and to a specified reserve area. <BR><BR><BR>All ofstream constructors construct a filebuf object. The first three use an internally allocated reserve area, but the fourth uses a user-allocated area. The user-allocated area is not automatically released during destruction.<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Norton AntivVirus 回复日期:2002-10-30 11:37:39
<br>内容:ofstream file("text.txt",ios::trunc);<BR><BR>这个本来就可以用,搞什么东东嘛!<BR><BR>打开一次没有问题,只不过是每次重新打开的时候就从最前面写数据嘛!<BR><BR>你又没有讲清楚!<BR>2002-10-30 11:51:15
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -