由于某种原因,它取代了文本文件而不是推送它。所以我们有这个例子:
#include<iostream>
#include<string>
#include<fstream>
void puttingToText(
std::string things[3],
double thingsPoints[3]
){
std::ofstream Thefile("players.txt");
for(int i=0; i<=2; i++){
Thefile << things[i] << " : " << thingsPoints[i] << std::endl;
}
}
void printingItems(
std::string arrayHelpfull[3],
double arrayHelpfullPoints[3],
std::string arrayNone[3],
double arrayNonePoints[3],
std::string arrayHarmfull[3],
double arrayHarmfullPoints[3],
std::string option
) {
std::cout << option << std::endl;
if(option.compare("Plain") == 0){
std::cout << arrayNone[0]
<< " : "
<< arrayNonePoints[0]
<<std::endl
<< arrayNone[1]
<< " : "
<< arrayNonePoints[1]
<<std::endl
<< arrayNone[2]
<< " : "
<< arrayNonePoints[2]
<<std::endl;
puttingToText(arrayNone,arrayNonePoints);
}
else if(option.compare("Helpfull") == 0){
std::cout << arrayHelpfull[0]
<< " : "
<< arrayHelpfullPoints[0]
<<std::endl
<< arrayHelpfull[1]
<< " : "
<< arrayHelpfullPoints[1]
<<std::endl
<< arrayHelpfull[2]
<< " : "
<< arrayHelpfullPoints[2]
<<std::endl;
puttingToText(arrayHelpfull,arrayHelpfullPoints);
}
else{
std::cout << arrayHarmfull[0]
<< " : "
<< arrayHarmfullPoints[0]
<<std::endl
<< arrayHarmfull[1]
<< " : "
<< arrayHarmfullPoints[1]
<<std::endl
<< arrayHarmfull[2]
<< " : "
<< arrayHarmfullPoints[2]
<<std::endl;
puttingToText(arrayHarmfull,arrayHarmfullPoints);
}
}
int main(){
std::string arrayHelpfull[3] = {"fruits", "soda" , "candy"};
double arrayHelpfullPoints[3] = {20.4,50.2,30.0};
std::string arrayNone[3] = {"chair", "shoe" , "pencil"};
double arrayNonePoints[3] = {0,0,0};
std::string arrayHarmfull[3] = {"meth", "dirtyneedle","ninga"};
double arrayHarmfullPoints[3] = {-20,-50,-30};
int userChoice = 0;
while (userChoice != 4) {
std::cout << "1 - Just Plain Items"
<< std::endl
<< "2 - Helpfull Items"
<< std::endl
<< "3 - Harmfull Items"
<< std::endl
<< "4 - Quit"
<< std::endl;
std::cin >> userChoice;
switch (userChoice) {
case 1:
printingItems(
arrayHelpfull, arrayHelpfullPoints, arrayNone,
arrayNonePoints, arrayHarmfull, arrayHarmfullPoints, "Plain"
);
break;
case 2:
printingItems(
arrayHelpfull, arrayHelpfullPoints, arrayNone,
arrayNonePoints, arrayHarmfull, arrayHarmfullPoints, "Helpfull"
);
break;
case 3:
printingItems(
arrayHelpfull, arrayHelpfullPoints, arrayNone,
arrayNonePoints, arrayHarmfull, arrayHarmfullPoints, "Harmfull"
);
break;
}
}
}
你看...当我输入1例如它的剂量推动阵列,但替换它。
当我把有害的文本文件变成这样的时候:
meth : -20
dirtyneedle : -50
ninga : -30
接下来,当我把普通物品变成这样:
chair : 0
shoe : 0
pencil : 0
我想像这样推动它:
meth : -20
dirtyneedle : -50
ninga : -30
chair : 0
shoe : 0
pencil : 0
答案 0 :(得分:2)
您需要添加以下代码行才能将追加添加到文本文件而不是替换:
TheFile.open("FileName.Ext",ios::app);
这应该有效:
void puttingToText(
std::string things[3],
double thingsPoints[3]
){
std::ofstream Thefile;
Thefile.open("players.txt",ios::app);
for(int i=0; i<=2; i++){
Thefile << things[i] << " : " << thingsPoints[i] << std::endl;
}
}
答案 1 :(得分:1)
此行std::ofstream Thefile("players.txt");
打开要创建的文件,而不是附加。如果指定的文件已存在,则将其删除并覆盖。
有关详情,请参阅ofstream documentation。