作者ntueestudent (bluesky)
看板EE_DSnP
标题[问题] 关於存档的问题
时间Sun Jan 20 13:00:58 2008
不知道为什麽
我一按存档键我要存的档就被清空了= =
这是我的code:
void saveFile()
{
fout.open(fileName); //一到这行就被清空.....
if(!fout)
{
sstr << "No file is opened." << '\n';
print();
}
else
{
fout << ssbuffer.str();
ssbuffer.str(""); // 清空ssbuffer
sstr << "File is now saved." << '\n';
print();
}
}
补充︰ ofstream fout
stringstream ssbuffer (这是拿来存所有edit内容的)
(我确定里面真的有东西)
char fileName[100]
另外
我写了个小程式来测试:
int main()
{
char filename[10];
cin >> filename;
ofstream fout;
fout.open(filename);
if ( !fout )
{
cerr << "File could not be opened" << endl;
exit( 1 );
}
cout << "Enter the text you want to input." << endl;
string str;
stringstream sstr,sstr2;
cin >> str ;
sstr << str ;
fout << sstr.str();
system("pause");
return 0;
}
这是OK的!!
顺便问一下
我的fileName 第一次输入是成功的
可是第二次以後就不行了
这是为什麽??
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 122.121.99.48
※ 编辑: ntueestudent 来自: 122.121.99.48 (01/20 13:10)
1F:→ ntueestudent:fileName的问题解决了~不过谁能告诉我为什麽ssbuffer 01/20 16:38
2F:→ ntueestudent:存进去什麽都没有阿> < 01/20 16:40
3F:→ keyboardle:请问在你saveFile()下方的]是本来就这样吗= = 01/20 18:29
4F:→ ntueestudent:没有啦~不小心打错而已 01/20 19:26
※ 编辑: ntueestudent 来自: 218.168.96.236 (01/20 19:29)
5F:推 timrau:Try to add ssbuffer.clear() before ssbuffer.str("") 01/20 20:56
6F:→ ntueestudent:可以了^^ 感谢楼上大大~~ 不过为什麽阿??? 01/20 21:46
7F:推 timrau:清掉一些指示"这个stringstream不能再被用"的flag 01/20 22:10