我是新手 谢谢 关于写文件的问题 急 大概意思就是这样 写1.txt然后再次打开1.txt 向里写入文字我想用CFile.Write 写但是每次都是覆盖原来写的文件不知道如何接着上文写文件时候Write里还有什么参数?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 CFile f;f.Open("1.txt", CFile::modeCreate | CFile::modeNoTruncate); 你再次打开文件后,在写数据之前用SeekToEnd,然后再写就可以接着以前的内容写了SeekToEnd是把文件指针移动到文件最后 要想接上文写文件的话,f.SeekToEnd();f.Write(pBuf, iLength);就OK了 或者你用: CFile f; f.SeekToEnd();然后在写文件。 CFile MyFile("c:\\test.txt",CFile::modeReadWrite|CFile::modeNoTruncate); MyFile.SeekToEnd(); char buff[]="test"; MyFile.Write(buff,strlen(buff)): MyFile.Close;这样就可以了 SeekToEnd()Seek()或者用:CArchive中的serialize就是直接写在最后的 直接用Api函数比较容易实现文件的输入输出 用CFile::modeCreate|CFile::modeReadWrite时要注意,如果要open的文件已经存在,那CFile::modeCreate就会把文件的长度设置为0,所以你每回都是从文件头开始写(详细情况看MSDN)。你可以用下面的代码试试:if (!file.Open (m_strErrorFile,CFile::modeReadWrite)) if (!file.Open(m_strErrorFile, CFile::modeCreate|CFile::modeReadWrite)) {::AfxMessageBox ("打开文件失败");return;}//ok啦。第一个if是指如果原文件存在的话就打开,如果不存在就跳到第二个if去,再进行创建新文件,如果创建不成功,就提示,成功就继续。 LeoKing(白景孜) :用不着那么麻烦只要再加一个 CFile::modeNoTruncate); 就可以了就像下面这样不管test.txt文件存在不存在,如果不存在,就创建一个新的,如果存在,就打开而不把文件长度重新设置CFile MyFile("c:\\test.txt",CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate); MyFile.SeekToEnd(); char buff[]="test"; MyFile.Write(buff,strlen(buff)); MyFile.Close; snsins(不再信仰,不再坚持):哦,我试试:0谢谢你的提示:) CFile::modeReadWrite参数在open时会将文件长度置为0,所以再加一个CFile::modeNoTruncate参数,就不会置0了。 # Include< fstream.h>class WW{....void operater << (ostream oo, fstream s){s<<oo;...... }void main() {fstream f;//open a file f.open ("1.dat",ios::in|ios::out);....} CreateFile();ReadFile();WriteFile(); 也可以用流来写文件:ofstream( const char* szName, int nMode = ios::out, int nProt = filebuf::openprot ); 其实主要的问题在于打开文件时:CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate,查找msdn或者一些资料,看CFILE的成员函数即可.(一家之言) 后台模拟键盘按键,在记事本中模拟 字母键 没反应 这个错误 怎么办?? 文档视图界面关闭时出现错误? debug和release编译环境的区别的问题 为什么要删除我的这个帖子,还扣分!!! 有没有人研究小波压缩中的EBCOT编码? 怎样限制CMonthCalCtrl控件输入日期的范围 C++发信程序伪装邮箱被QQ系统拦截成垃圾邮件? 大神帮忙,网页中文字符处理问题,着急着急!!! 一个关于提示框的小问题,拿分机会哦! 如何获得给定的文件的路径,比如文件"a.txt"发路径? 求救!!
f.Open("1.txt", CFile::modeCreate | CFile::modeNoTruncate);
f.SeekToEnd();
f.Write(pBuf, iLength);
就OK了
CFile f;
f.SeekToEnd();
然后在写文件。
MyFile.SeekToEnd();
char buff[]="test";
MyFile.Write(buff,strlen(buff)):
MyFile.Close;这样就可以了
Seek()
或者用:CArchive中的serialize就是直接写在最后的
你可以用下面的代码试试:
if (!file.Open (m_strErrorFile,CFile::modeReadWrite))
if (!file.Open(m_strErrorFile,
CFile::modeCreate|CFile::modeReadWrite))
{::AfxMessageBox ("打开文件失败");return;}
//ok啦。
第一个if是指如果原文件存在的话就打开,如果不存在就跳到第二个if去,再进行创建新文件,如果创建不成功,就提示,成功就继续。
用不着那么麻烦
只要再加一个 CFile::modeNoTruncate); 就可以了
就像下面这样
不管test.txt文件存在不存在,如果不存在,就创建一个新的,如果存在,就打开而不把文件长度重新设置CFile MyFile("c:\\test.txt",CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate);
MyFile.SeekToEnd();
char buff[]="test";
MyFile.Write(buff,strlen(buff));
MyFile.Close;
哦,我试试:0
谢谢你的提示:)
class WW
{....
void operater << (ostream oo, fstream s)
{s<<oo;......
}void main()
{fstream f;
//open a file
f.open ("1.dat",ios::in|ios::out);....}
ReadFile();
WriteFile();
ofstream( const char* szName, int nMode = ios::out, int nProt = filebuf::openprot );