无法从文本文件中提取一些记录

时间:2012-03-16 10:20:36

标签: c++ file text-files extract dev-c++

每次满足某些条件时,我都需要从包含数据的文本文件中提取和显示记录。问题是当我从文本文件中提取它们时,会省略一些记录。任何帮助将不胜感激。我的代码如下,用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

2 个答案:

答案 0 :(得分: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();

从文件流读取字符串流中的数据有点多余,但它与您开始使用的示例很接近。


如果你想使用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);

答案 1 :(得分:0)

您似乎已经阅读了汽车的属性,如果匹配,则打印下一行。您可能想要确保的另一件事是您编写的值不包含空格。在为您写入值时,您可能应该确保值之间至少有一个空格。

我发现您使用eof()作为循环中的条件:这绝对是错误的,您只想使用隐式转换为booleof()的唯一用途是在读取失败时禁止错误报告,因为您已到达文件的末尾。

相关问题