2014-11-05 136 views
1

我試圖從名爲「parking.txt」的文件讀入,並且我想從該文件讀取某些值並將它們輸出到屏幕。如何才能做到這一點?這可以做到嗎?如何從C++中的文件讀取

值在parking.txt是:

total 5 
One 400 
Five 300 
Ten 200 
Twenty 50 
Quarter 500 

在我的代碼,我想從文件中的相應值來代替「行」。

#include <iostream> 
#include <fstream> 
#include <iomanip> 

using namespace std; 

int main() 
{ 
    ifstream inputFile ("parking_account.txt"); 
    string line; 

    getline(inputFile, line); 

    cout <<"\n\t-------------------------------------------------------"; 
    cout <<"\n\t======================================================="; 
    cout <<"\n\t    Parking Machine Accounts     "; 
    cout <<"\n\t======================================================="; 
    cout <<"\n\tSr. No. : Bill Name  : Bill Count : Cost(in$) "; 
    cout <<"\n\t-------------------------------------------------------"; 
    cout <<"\n\t  1 : One Dollar  : " << line << " : "; 
    cout <<"\n\t  2 : Five Dollar  : " << line << " : "; 
    cout <<"\n\t  3 : Ten Dollar  : " << line << " : "; 
    cout <<"\n\t  4 : Twenty Dollar : " << line << " : "; 
    cout <<"\n\t  5 : Quarter   : " << line << " : "; 

    cout<<"\n\tTotal bill types found : " <<line <<endl; 
} 

我已經嘗試了while循環搜索一行行,但它與輸出線相同的菜單更新的文本值的5。這是while循環。

int main() 
{ 
    ifstream inputFile ("parking_account.txt"); 
    string line; 

    getline(inputFile, line); 
    while (inputFile) 
    { 
     cout <<"\n\t-------------------------------------------------------"; 
     cout <<"\n\t======================================================="; 
     cout <<"\n\t    Parking Machine Accounts     "; 
     cout <<"\n\t======================================================="; 
     cout <<"\n\tSr. No. : Bill Name  : Bill Count : Cost(in$) "; 
     cout <<"\n\t-------------------------------------------------------"; 
     cout <<"\n\t  1 : One Dollar  : " << line << " : "; 
     cout <<"\n\t  2 : Five Dollar  : " << line << " : "; 
     cout <<"\n\t  3 : Ten Dollar  : " << line << " : "; 
     cout <<"\n\t  4 : Twenty Dollar : " << line << " : "; 
     cout <<"\n\t  5 : Quarter   : " << line << " : "; 

     cout<<"\n\tTotal bill types found : " <<line <<endl; 
     getline(inputFile, line); 
    } 
} 
+0

問題和摘要是指不同的輸入文件,解決這個問題 – Basilevs 2014-11-05 03:55:54

+0

也從循環中移除示例輸出,它用於演示,而不是重用。 – Basilevs 2014-11-05 03:59:12

回答

1

嘗試使用提取運算符>>

string dummy; //this holds those separators since I have assumed that the numbers are always in the same order 
//alternately, you could extract this two `>>`'s at a time, processing the string that 
//comes befor the number to determine where it should go. For simplicity, I have 
//assumed that the order is always the same. 

int total one, five, ten, twenty, quarter; 
inputFile >> dummy >> total >> dummy >> one >> dummy >> five >> dummy >> ten >> dummy >> twenty >> dummy >> quarter; 

這樣做是先提取你的 「總」 串入dummy。接下來,它將值「5」提取爲整數total。在此之後,它將「1」提取爲dummy,400,將one作爲整數,將「2」提取爲dummy,將「300」提取爲作爲整數的five等等。如果我錯誤地解釋了你的字符串格式,它應該很簡單,以修改上面的匹配。

然後,您可以取代你line變量與相應的變量的輸出保持你對你的表(onefive等),感興趣的值。

>>運算符由istream提供,對於這些類型的場景很有用。 (它是有用的注意,這適用於cin,以及因爲cin的類是從istream下降,就像ifstreamistream下降)

0

你應該檢查文件是否可以打開。 如果您可以打開文件,請將文件中的值讀入您的變量。 你可以做這樣的事情:

如果這是parking_account.txt

5 400 300 200 50 500 

這是的main.cpp

#include <iostream> 
#include <fstream> 
#include <iomanip> 

using namespace std; 

int main() 
{ 
    ifstream inputFile("parking_account.txt"); 
    string line = ""; 
    int total = 0; 
    int one = 0; 
    int five = 0; 
    int ten = 0; 
    int twenty = 0; 
    int quarter = 0; 

    if (!inputFile.is_open()) { 
    cerr << "Could not read from file" << endl; 
    } 
    else { 
    inputFile >> total >> one >> five 
    >> ten >> twenty >> quarter; 
    } 
    getline(inputFile, line); 

    cout <<"\n\t-------------------------------------------------------"; 
    cout <<"\n\t======================================================="; 
    cout <<"\n\t    Parking Machine Accounts     "; 
    cout <<"\n\t======================================================="; 
    cout <<"\n\tSr. No. : Bill Name  : Bill Count : Cost(in$) "; 
    cout <<"\n\t-------------------------------------------------------"; 
    cout <<"\n\t  1 : One Dollar  : " << one << " : "; 
    cout <<"\n\t  2 : Five Dollar  : " << five << " : "; 
    cout <<"\n\t  3 : Ten Dollar  : " << ten << " : "; 
    cout <<"\n\t  4 : Twenty Dollar : " << twenty << " : "; 
    cout <<"\n\t  5 : Quarter   : " << quarter << " : "; 

    cout<<"\n\tTotal bill types found : " << total <<endl; 

    return 0; 
}