2012-03-16 8 views
1

いくつかの条件が満たされるたびに、データを含むテキストファイルからレコードを抽出して表示する必要があります。問題は、一部のレコードがテキストファイルから抽出されたときに省略されていることです。どんな助けもありがとう。私のコードは、のDev-C++で書かれた、以下の通りです:テキストファイルから一部のレコードを抽出できません

#include <iostream> 
#include <conio.h> 
#include <cstring> 
#include <fstream> 
#include <iomanip> 
#include <string> 


using namespace std; 

int main() 
{ 
    char manufacturer[16], model[16], year[10]; 
    int miles,car_cost; 
    char response, line[256]; 
    string field1, field2, field3; 

    int MilesText ,car_costText; 
    ofstream OS ("usedcars.txt", ios::out); 
    cout<<"for each car please enter :"<<endl; 

    do 
    { 
    ofstream OS ("usedcars.txt", ios::app); 

    cout<<"The manufacturer: "; 
    cin.getline(manufacturer, 16); 
    cout<<"The model: "; 
    cin.getline(model, 16); 
    cout<<"The year: "; 
    cin.getline(year, 8); 
    cout<<"The miles: "; 
    cin>>miles; 
    cin.ignore(); 
    cout<<"The cost of car $: "; 
    cin>>car_cost; 

    OS<< manufacturer << setw(9) << model << setw(8) << year << setw(11) << miles << setw(8) << car_cost<<endl; 
    cout<<"Do you want to continue?"; 
    cin>>response; 
    cin.ignore(); 
    } 
    while (response!='n'); 
    OS.close(); 
    cout<<"-------------------------------------"<<endl; 
    cout<<"the record found"<<endl; 
    ifstream IS ("usedcars.txt", ios::in); 
    while(!IS.eof()) 
    { 
     IS>>field1>>field2>>field3>>MilesText>>car_costText; 
     if(MilesText<50000 && car_costText<9000) //if the miles is less than 50000 and  the cost is less than 9000 therefore... 

    while(IS) 
     { 
     IS.getline(line, 256); 
     if(IS) 
     cout<<line<<endl; //display the record from text file 
    } 
} 
IS.close(); 
getch(); 
return 0; 
} 

**********************出力******** ************************************

for each car please enter : 
The manufacturer: Mitsubishi 
The model: Lancer 
The year: 2001 
The miles: 12300 
The cost of car $: 10780 
Do you want to continue?y 
The manufacturer: Ford 
The model: Escape 
The year: 2004 
The miles: 150000 
The cost of car $: 6200 
Do you want to continue?y 
The manufacturer: Audi 
The model: A4 
The year: 1999 
The miles: 79000 
The cost of car $: 11000 
Do you want to continue?n 

レコードが

を見つけましたテキストファイル内の

************************* ******************* *******************

Mitsubishi Lancer 2001  12300 10780 
Ford Escape 2004  150000 6200 
Audi  A4 1999  79000 11000 
Volvo  S80 1998  14000 7900 
+0

一方(IS) {IS.getline(ライン256)。 if(IS)は論理を理解していません。あなたは明確にすることができますか? –

+0

なぜ構造体やクラスを使用できないのですか? –

+0

まだファイルにデータがある限り、その行を抽出してください。 – T4000

答えて

1

少なくとも、あなたが正しくやりたいことを理解していれば、あなたの問題(入力ファイルを読む部分)の解決策がいくつかあります。私にとって、それは入力ファイルのうち、< 50000と9000以下の価格ですべての車を印刷します。

#include <sstream> 
// rest of your code... 

ifstream IS ("usedcars.txt", ios::in); 
string lineTextfile; 

while(IS) 
{ 
    getline(IS, lineTextfile); // read one line of input file 

    istringstream parseLine(lineTextfile); 

    parseLine>>field1>>field2>>field3>>MilesText>>car_costText; // parse individual elements of that line 

    if(MilesText<50000 && car_costText<9000) //if the miles is less than 50000 and  the cost is less than 9000 therefore... 
    { 
     cout<<lineTextfile<<endl; //display the record from text file 
    } 
} 

IS.close(); 

FILESTREAM にstringstreamからのデータの読み出しがやや冗長であるが、それはあなたが開始した例に近接しています。


あなたは、このようにストリーム演算子をオーバーロードするために、このコードを使用することができstructを使用したい場合:

struct Car 
{ 
    Car() : year(0), mileage(0), price(0) {}; 
    string manufacturer; 
    string model; 
    unsigned int year; 
    unsigned int mileage; 
    unsigned int price; 
}; 

istream& operator>>(istream& is, Car& car) 
{ 
    is >> car.manufacturer >> car.model >> car.year >> car.mileage >> car.price; 
    return is; 
} 

ostream& operator<<(ostream& os, const Car& car) 
{ 
    os << car.manufacturer << " " << car.model << " " << car.year << " " << car.mileage << " " << car.price; 
    return os; 
} 

bool IsFairPricedCar(const Car& car) 
{ 
    return car.price < 9000 && car.mileage < 50000; 
} 

そのstruct定義と、次が可能です:

ifstream IS ("usedcars.txt", ios::in); 

while (IS) 
{ 
    Car readCar; 
    IS >> readCar; 
    if(readCar.mileage < 50000 && readCar.price < 9000) 
    { 
     cout << readCar << endl; 
    } 
} 

IS.close(); 

代替品

ifstream IS2 ("usedcars.txt", ios::in); 

copy_if(istream_iterator<Car>(IS2), istream_iterator<Car>(), 
    ostream_iterator<Car>(cout, "\n"), IsFairPricedCar); 
0

あなたがあなたの車の性質を読み、それらが一致する場合は、次の行を印刷するようです。あなたが書いた値にスペースが含まれていないことを確認しておきたいこともあります。値を書き込む際には、値の間に少なくとも1つのスペースがあることを確認する必要があります。

私はあなたがループ内で条件のためeof()を使用することを発見:これは間違いなく間違っていると、あなたはboolにだけ暗黙的な変換を使用します。 eof()の唯一の用途は、ファイルの最後に達したために読み込みに失敗したときのエラーレポートを抑制することです。

関連する問題