You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C++逐行逐词读取单词数量不均的汽车信息TXT文件?

如何用C++逐行逐词读取单词数量不均的TXT文件?

嘿,这个场景太常见了——处理这种每行字段数量不一样的结构化文本,用C++标准库就能轻松搞定。我给你分享两种实用的思路,你可以根据自己的代码风格来选:

方法一:先读整行,再拆分字段

这种方法的核心是先把每行完整读进来,再用字符串流拆分里面的单词,然后根据Type的值决定要读取多少个后续字段。好处是能明确对应每一行的数据,避免跨行列的问题。

先看完整的代码示例:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main() {
    ifstream inFile("cars.txt"); // 替换成你的文件名
    if (!inFile.is_open()) {
        cerr << "哎呀,打不开文件!" << endl;
        return 1;
    }

    string line;
    while (getline(inFile, line)) { // 逐行读取整行内容
        istringstream lineStream(line);
        int type;
        string name;
        int buyPrice;
        // 先读取前两个必有的字段:Type和Name
        lineStream >> type >> name;

        switch (type) {
            case 1: {
                int rentPrice;
                lineStream >> buyPrice >> rentPrice;
                cout << "Type 1 汽车:" << name << ",购买价:" << buyPrice << ",租赁价:" << rentPrice << endl;
                break;
            }
            case 2:
            case 3: {
                lineStream >> buyPrice;
                cout << "Type " << type << " 汽车:" << name << ",购买价:" << buyPrice << endl;
                break;
            }
            case 4: {
                int rentPrice, insurancePrice;
                lineStream >> buyPrice >> rentPrice >> insurancePrice;
                cout << "Type 4 汽车:" << name << ",购买价:" << buyPrice << ",租赁价:" << rentPrice << ",保险价:" << insurancePrice << endl;
                break;
            }
            default:
                cerr << "未知的汽车类型:" << type << ",行内容:" << line << endl;
                break;
        }
    }

    inFile.close();
    return 0;
}

关键点说明:

  • getline(inFile, line):把文件里的每一行完整读取到字符串line里,不管这行有多少个单词。
  • istringstream lineStream(line):把整行字符串转换成一个输入流,这样就能像操作cin一样逐个提取单词了。
  • 分支处理:因为不同Type的字段数量不同,用switch分别处理,确保每个类型都读取对应数量的字段,不会漏读或多读。

方法二:直接逐词读取,按类型分组

如果你确定文件里的每个汽车数据都是连续的(不会跨行列混着),也可以跳过读整行的步骤,直接从文件流里逐个读取单词,先拿到Type,再根据Type读取后续的字段。

代码示例如下:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    ifstream inFile("cars.txt");
    if (!inFile.is_open()) {
        cerr << "文件打开失败,请检查路径!" << endl;
        return 1;
    }

    int type;
    while (inFile >> type) { // 先读取Type,只要能读到就继续循环
        string name;
        inFile >> name;

        switch (type) {
            case 1: {
                int buy, rent;
                inFile >> buy >> rent;
                cout << "Type 1: " << name << ", 购买价:" << buy << ", 租赁价:" << rent << endl;
                break;
            }
            case 2:
            case 3: {
                int buy;
                inFile >> buy;
                cout << "Type " << type << ": " << name << ", 购买价:" << buy << endl;
                break;
            }
            case 4: {
                int buy, rent, insurance;
                inFile >> buy >> rent >> insurance;
                cout << "Type 4: " << name << ", 购买价:" << buy << ", 租赁价:" << rent << ", 保险价:" << insurance << endl;
                break;
            }
            default:
                cerr << "无效的类型:" << type << endl;
                // 跳过当前行剩下的内容,避免影响下一个数据
                string trash;
                while (inFile >> trash);
                break;
        }
    }

    inFile.close();
    return 0;
}

注意事项:

  • 这种方法依赖于文件数据的规范性,如果某一行的Type后面的字段不完整,可能会导致后续读取混乱,所以最好加上错误检查(比如判断每个字段是否读取成功)。
  • 遇到无效Type时,记得跳过当前行剩下的内容,不然会把下一行的内容当成当前行的剩余字段来读,导致逻辑错乱。

额外小提示

  • 不管用哪种方法,都要记得检查文件是否成功打开,避免程序崩溃。
  • 如果需要把数据存储起来,建议定义一个基类+派生类的结构,或者用struct配合C++17的std::variant来存放不同类型的汽车数据,这样代码会更整洁。

内容的提问来源于stack exchange,提问作者Joe K.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:02:54