2016-04-26 42 views
-1

由于某种原因,它取代的,而不是通过它推..文本文件所以我们有这样的例子:替换文本文件,而不是推数据

#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

您是否尝试过检查它是否正确打开? –

+0

是的,我做了多次@FirstStep。你可以在cloud9上看到它:https://ide.c9.io/uniforlyff/unifly – amanuel2

+3

尝试添加这行代码'TheFile.open(“FileName.Ext”,ios :: app);'在写入之前文件循环 –

回答

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

或者你可以把这个:std :: ofstream Thefile(“players.txt”);在文件的顶部..所以它剂量替代..两个作品!再次感谢您的时间!(3分钟左右) – amanuel2

+0

很高兴帮助。但是有一个问题,你把它放在文件顶部是什么意思?你的意思是作为一个全局变量?因为它已经是函数no的第一行了? –

+0

在顶部我的意思是使它成为一个全局变量..而且它不是一个全局变量,变量(已经说变量三次...)在函数内部。所以你需要把变量放在最上面。这里是我所做的代码:https://github.com/amanuel2/C-Examples/blob/master/Games/File-Handling/pt1/main.cpp – amanuel2

1

此行std::ofstream Thefile("players.txt");打开文件创造,不追加。如果指定的文件已经存在,它将被删除并被覆盖。

查看ofstream documentation了解更多。