![](/img/trans.png)
[英]C++ How do I make it so my program wouldn't delete files that are in use?
[英]My program input wouldn't save into a text file
大家好,我在下面写的代码是一个日记项目,它仍处于早期阶段,因此在运行它时,它应该告诉您输入日记条目,然后将其保存在文本中,但未保存在其中。文件请帮忙!!
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
ofstream wysla;
wysla.open("wysla.txt, ios::app");
int kaput;
string s1,s2;
cout<<"Please select from the List below"<<endl;
cout<<"1.New entry"<<endl;
cout<<"2.View Previous Entries"<<endl;
cout<<"3.Delete an entry"<<endl;
cin>>kaput;
switch (kaput)
{
case 1:
cout<<"Dear diary,"<<endl;
cin>>s1;
wysla<<s1;
wysla.close();
break;
}
return 0;
}
我想你这里有错字
wysla.open("wysla.txt", ios::app);
除了丹尼尔·A·怀特(Daniel A. White)的答案之外,您可能还会遇到问题,即文件中仅保存了一个单词。 这是由以下行引起的:
cin>>s1;
你宁可使用
getline(cin, s1);
这里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.