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

C++中汽车信息的文件读写实现技术咨询

汽车信息文件读写的C++实现方案

一、先定义基础结构体

首先明确car结构体的核心属性,适配需求中的数据存储:

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

using namespace std;

struct car {
    string model;       // 汽车型号
    string licensePlate;// 车牌
    string color;       // 颜色
    // 可根据需求扩展其他属性
};

二、文件写入功能完善

补全你提供的WriteToFile函数的循环写入逻辑,同时优化格式以适配后续读取需求:

void WriteToFile(car* current_car[], const string& type, int numberOfCars, const string& fileName) {
    ofstream file(fileName);

    if (!file.is_open()) {
        cerr << "打开文件失败:" << fileName << endl;
        return;
    }

    // 写入表头:按要求用英文句点分隔类型与数量
    file << type << "." << numberOfCars << endl;

    // 循环遍历所有汽车,写入细节(属性间用句点分隔)
    for (int i = 0; i < numberOfCars; ++i) {
        if (current_car[i] == nullptr) continue; // 跳过空指针,避免崩溃

        file << current_car[i]->model << "."
             << current_car[i]->licensePlate << "."
             << current_car[i]->color << endl;
    }

    file.close();
}

写入功能说明

  • 补充了原函数缺失的返回类型,添加了文件打开失败的错误提示
  • 表头格式改为[汽车类型].[车辆数量],满足读取时的分隔要求
  • 循环遍历传入的汽车指针数组,将每辆车的型号、车牌、颜色用句点分隔后写入文件
  • 增加空指针判断,防止访问非法内存导致程序崩溃

写入调用示例

int main() {
    // 示例数据:创建3辆汽车
    car sedan1 = {"Camry", "京A12345", "白色"};
    car sedan2 = {"Passat", "沪B67890", "黑色"};
    car suv1 = {"CR-V", "粤C01234", "灰色"};

    car* carList[] = {&sedan1, &sedan2, &suv1};
    int carNum = 3;
    string carCategory = "燃油车";

    // 写入到cars.txt文件
    WriteToFile(carList, carCategory, carNum, "cars.txt");

    // 后续可添加读取逻辑
    return 0;
}

三、文件读取功能实现

实现读取函数,解析文件中句点分隔的数据,将信息加载到程序中:

vector<car> ReadFromFile(const string& fileName, string& carType, int& carCount) {
    vector<car> carCollection;
    ifstream file(fileName);
    string line;

    if (!file.is_open()) {
        cerr << "打开文件失败:" << fileName << endl;
        return carCollection;
    }

    // 读取并解析第一行的表头信息
    getline(file, line);
    stringstream headerStream(line);
    getline(headerStream, carType, '.'); // 提取汽车类型

    string countStr;
    getline(headerStream, countStr, '.'); // 提取车辆数量字符串
    carCount = stoi(countStr); // 转换为整数

    // 读取并解析每辆车的细节
    while (getline(file, line)) {
        stringstream carStream(line);
        car newCar;

        getline(carStream, newCar.model, '.');
        getline(carStream, newCar.licensePlate, '.');
        getline(carStream, newCar.color, '.');

        carCollection.push_back(newCar);
    }

    file.close();
    return carCollection;
}

读取功能说明

  • 使用vector<car>存储读取到的汽车信息,相比数组更灵活,无需提前指定容量
  • 通过stringstream按英文句点分割每行数据,完成字符串到对应属性的解析
  • 表头行解析出汽车类型和数量,后续行解析每辆车的具体属性
  • 若文件打开失败,返回空vector,便于调用方判断读取结果

读取调用示例

在main函数中添加读取逻辑:

// 读取文件示例
string readCarType;
int readCarNum;
vector<car> readCars = ReadFromFile("cars.txt", readCarType, readCarNum);

// 输出读取结果
cout << "读取到的汽车类型:" << readCarType << endl;
cout << "读取到的汽车数量:" << readCarNum << endl;
cout << "汽车详情列表:" << endl;
for (const auto& c : readCars) {
    cout << "型号:" << c.model << " | 车牌:" << c.licensePlate << " | 颜色:" << c.color << endl;
}

内容的提问来源于stack exchange,提问作者ocrasoup

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 04:22:05