C++ C++;:逐行逐字读取txt文件,每行的字数不均匀

C++ C++;:逐行逐字读取txt文件,每行的字数不均匀,c++,C++,晚上好, 我有一个txt文件,其中包含一些汽车的信息。 根据其类型,每辆车都有自己的属性,例如: 类型1汽车有名称、购买价格、租赁价格。 Type(Int) Name(String) Buy(Int) Rent(Int) Insurance(Int) 1 toyota 5000 100 3 mazda 6000 130 2

晚上好,

我有一个txt文件,其中包含一些汽车的信息。 根据其类型,每辆车都有自己的属性,例如:

类型1汽车有名称、购买价格、租赁价格。

Type(Int)  Name(String)         Buy(Int)     Rent(Int)   Insurance(Int) 
1          toyota               5000         100   
3          mazda                6000         130
2          mitsubishi           10000   
1          honda                5000         110
4          ferrari              20000        220         1000
类型2汽车有名称和购买价格。

Type(Int)  Name(String)         Buy(Int)     Rent(Int)   Insurance(Int) 
1          toyota               5000         100   
3          mazda                6000         130
2          mitsubishi           10000   
1          honda                5000         110
4          ferrari              20000        220         1000
类型3汽车有名称和购买价格。

Type(Int)  Name(String)         Buy(Int)     Rent(Int)   Insurance(Int) 
1          toyota               5000         100   
3          mazda                6000         130
2          mitsubishi           10000   
1          honda                5000         110
4          ferrari              20000        220         1000
类型4汽车有名称、购买价格、租赁价格和保险价格。

Type(Int)  Name(String)         Buy(Int)     Rent(Int)   Insurance(Int) 
1          toyota               5000         100   
3          mazda                6000         130
2          mitsubishi           10000   
1          honda                5000         110
4          ferrari              20000        220         1000
现在,我只想阅读文件并打印每辆车的型号,这样我就知道我的代码是有效的。 到目前为止,我尝试的是:

ifstream carsFile(cars.txt);

string carType;
string carName;
string carBuyPrice;
string carRentPrice;
string carInsPrice;
string line;

while (getline(carsFile, line))
{
    istringstream buffer(line);

    while (buffer >> carType >> carName >> carBuyPrice >> carRentPrice >> carInsPrice)
    {
         if (carType == "1")
         {
            cout << "Car type 1" << endl;
         }
         else if (carType == "2")
         {
            cout << "Car type 2" << endl;
         }
         else if (carType == "3")
         {
            cout << "Car type 3" << endl;
         }
         else
         {
         cout << "Car type 4" << endl;
         }
    }
}

carsFile.close();
ifstream-carsFile(cars.txt);
字符串类型;
丝状肉桂;
字符串价格;
串结价格;
串珠;
弦线;
while(getline(carsFile,line))
{
istringstream缓冲区(行);
而(缓冲区>>卡式>>卡那姆>>卡布价格>>卡伦普莱斯>>卡伦普莱斯)
{
如果(carType==“1”)
{

cout当您执行
buffer>>carType>>carName>>carBuyPrice>>carRentPrice>>carInsPrice
时,您试图读取所有这些信息而不检查它们是否存在,导致输入失败,循环体无法执行。为什么您首先需要while循环

您应该首先输入汽车的类型,然后使用一些if语句或switch语句来决定对每种汽车做什么

while(getline(carsFile, line))
{
    istringstream buffer(line);
    int type, rent, insurance;
    string name;
    buffer >> type >> name;
    switch(type)
    {
    case 1:
        buffer >> rent;
        //...
    case 2:
        //...
    }
}

当您执行
buffer>>carType>>carName>>carBuyPrice>>carRentPrice>>carInsPrice
时,您试图读取所有这些信息而不检查它们是否存在,从而导致输入失败,循环体无法执行。为什么首先需要while循环

您应该首先输入汽车的类型,然后使用一些if语句或switch语句来决定对每种汽车做什么

while(getline(carsFile, line))
{
    istringstream buffer(line);
    int type, rent, insurance;
    string name;
    buffer >> type >> name;
    switch(type)
    {
    case 1:
        buffer >> rent;
        //...
    case 2:
        //...
    }
}

我强烈建议为文件中的记录建模一个结构。接下来,重载
operator>
以读取字段。
例如:

struct Car_Info
{
  int type;
  std::string manufacturer;
  int buy_price;
  int rent_price;
  int insurance_price;
// Here's the kicker
  friend std::istream&  operator>>(std::istream& input, Car_Info& ci);
};

std::istream&  operator>>(std::istream& input, Car_Info& ci)
{
  std::string text_line;
  std::getline(input, text_line);
  if (input)
  {
    std::istringstream text_stream(text_line);
    // Initialize optional fields
    ci.rent_price = 0;
    ci.insurance_price = 0;
    text_stream >> ci.type
        >> ci.manufacturer
        >> ci.buy_price;
        >> ci.rent_price
        >> ci.insurance_price;
    }
}
您的输入循环将如下所示:

std::vector<Car_Info> database;
Car_Info car;
while (input_file >> car)
{
   database.push_back(car);
}
std::向量数据库;
汽车信息汽车;
while(输入_文件>>汽车)
{
数据库。推回(汽车);
}
该结构取代了并行阵列,减少了同步错误造成的缺陷


字符串用于读取一条文本记录。任何读取问题(如eof)都会改变输入流的状态,因此stringstream用于隔离丢失字段产生的错误。

我强烈建议对文件中的记录建模。接下来,重载
操作符>
读取字段。
例如:

struct Car_Info
{
  int type;
  std::string manufacturer;
  int buy_price;
  int rent_price;
  int insurance_price;
// Here's the kicker
  friend std::istream&  operator>>(std::istream& input, Car_Info& ci);
};

std::istream&  operator>>(std::istream& input, Car_Info& ci)
{
  std::string text_line;
  std::getline(input, text_line);
  if (input)
  {
    std::istringstream text_stream(text_line);
    // Initialize optional fields
    ci.rent_price = 0;
    ci.insurance_price = 0;
    text_stream >> ci.type
        >> ci.manufacturer
        >> ci.buy_price;
        >> ci.rent_price
        >> ci.insurance_price;
    }
}
您的输入循环将如下所示:

std::vector<Car_Info> database;
Car_Info car;
while (input_file >> car)
{
   database.push_back(car);
}
std::向量数据库;
汽车信息汽车;
while(输入_文件>>汽车)
{
数据库。推回(汽车);
}
该结构取代了并行阵列,减少了同步错误造成的缺陷


字符串用于读取一条文本记录。任何读取问题(如eof)都将更改输入流的状态,因此stringstream用于隔离丢失字段所产生的错误。

内部while循环的条件是流的状态(转换为布尔表达式)执行所有格式化输入操作后。如果其中任何一个操作失败,流将被标记为错误,导致条件评估为
false

您需要单独检查输入操作。

出于演示目的:

while(std::getline(std::cin, line)) {
  std::cout
    << "have \"" << line << "\"\n"
    << "read together:\n";
  read_together(line);
  std::cout << "read separately:\n";
  read_separately(line);
}
while(std::getline(std::cin,line)){
标准::cout

>b)std::cout内部while循环的条件是执行所有格式化输入操作后流的状态(转换为布尔表达式)。如果其中任何一个操作失败,流将被标记为错误,导致条件计算为
false

您需要单独检查输入操作。

出于演示目的:

while(std::getline(std::cin, line)) {
  std::cout
    << "have \"" << line << "\"\n"
    << "read together:\n";
  read_together(line);
  std::cout << "read separately:\n";
  read_separately(line);
}
while(std::getline(std::cin,line)){
标准::cout

>b)std::cout你的第二个
,而
需要所有读取才能工作,否则它将返回false。你必须将其分为多个输入,并分别检查每个输入的成功/失败。你能再详细说明一下吗?我如何准确地将其分为多个输入?在互联网上搜索“StAdvExc++ C++读取文件结构空间分隔”。第二版代码> 要求所有的读操作,否则返回false。您必须将它分成几个输入,并逐个检查每个成功/失败。您能详细说明一下吗?我如何准确地把它分成几个输入?搜索Internet。StAcExpLoad C++读取文件结构空间分隔开。