2017-04-07 75 views
0

目前我想从一个文件,是在fstream的阅读从一个文件中的数字输出,并且输出总和到另一个文件

  • 格里的格式读取$ 300
  • 托姆$ 500强
  • 格斯$ 700
  • 并试图将总金额输出到另一个文件。

    我该怎么办?所有帮助非常感谢!

    +0

    我知道如何成功地打开INFILE,阅读它,打开不过outFile并输出到它,我只是不知道如何将总金额加在一起显示。 –

    +0

    发布你的代码会给你更好的回应。我们不是来做你的家庭作业,但会帮助你陷入困境。 – soulsabr

    回答

    0

    既然你说你知道如何输入和输出到文件已经,你只需要做输入和输出之间的计算。因此,将值输入到变量中,然后添加变量并将结果存储在另一个变量中。将结果变量输出到新文件。

    例如:

    inData >> a >> b >> c; 
    result = a + b + c; 
    outData << result << endl; 
    
    0

    定义输入文件流和打开文件

    ifstream file("file directory"); 
    

    现在我们一行一行的,所以我们需要一个字符串保存每一行读它

    string line; 
    

    当到达文件末尾时,getline函数将返回false

    while(getline(file,line)){ 
          // you can use a string stream to easily divide the string according to spaces 
          istringstream ss(line); //now it contains your line 
          string word; 
    
          //now every time you do ss>>word you will get a the next word after space so you can make a loop and stop at the index of word u like to 
    while(ss>>word){ 
    //some code to stop when you find your word } 
    

    } 现在只是做一个变量,不断增加的数字给它,并最终将其打印输出文件流

    相关问题