然后程序应该访问/创建一个报告文件(称为output.txt),其中将保存此计算的结果。 请注意,如果“output.txt”文件已经存在,那么程序应该通过添加此计算结果来附加现有数据。
while(choice==1){
int choice1;
menu();
cin>>choice1;
cout<<endl;
if (choice1==2){
int num, counter=0;
int node=0;
cout<<"Please give the node you are looking for: ";
cin>>node;
for (num=0;num<arcs;num=num+2){
if (node==tail[num]) {
counter++;
}
}
cout<<"There are "<<counter<<" arcs leaving node "<<node;
cout<<endl;
outfile.open("output.txt");
if(outfile.is_open()){
outfile<<"There are "<<counter<<" arcs leaving node "<<node<<endl;
}
outfile.close();//when I choose option 2 again it replaces the previous one
cout<<"Report stored in file: output.txt."<<endl<<endl;
当我第二次选择选项2时,先前的输出被新的输出替换。
答案 0 :(得分:0)
这应该将文字添加到文件的末尾。
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream addtext;
addtext.open ("testdoc.txt", fstream::in | fstream::out | fstream::app);
addtext<<"testTestTest"<<endl;
addtext.close();
return 0;
}