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

C++实现读取文本文件数据存入主程序并行数组问题排查

问题说明

需要实现功能函数,从文本文件cards.txt读取卡牌信息,通过指针将数据写入主程序定义的并行数组。当前文本文件读取逻辑可正常运行,但读取到的信息无法存入对应数组,原有实现代码如下:

#include <iostream>
#include <stream>
#include <string>
using namespace std;

void readCards();

int main() {
  ifstream inputFile;
  const int SIZE = 10;
  int id[SIZE];
  string beybladeName[SIZE];
  string productCode[SIZE];
  string type[SIZE];
  string plusMode[SIZE];
  string system[SIZE];

  readCards();

  return 0;
}

void readCards() {
  ifstream inputFile;
  const int SIZE = 10;
  int id[SIZE];
  string beybladeName[SIZE];
  string productCode[SIZE];
  string type[SIZE];
  string plusMode[SIZE];
  string system[SIZE];
  int i = 0;

  inputFile.open("cards.txt");
  cout << "Reading all cards information..." << endl;
  if (inputFile) {
    while (inputFile >> id[i] >> beybladeName[i] >> productCode[i] >> type[i] >> plusMode[I] >>
      system[I]) {
      i++;
    }
    cout << "All cards information read." << endl;
  }
  inputFile.close();

  for (int index = 0; index < SIZE; index++) {
    cout << "#:" << id[index] << endl;
    cout << "Beyblade Name: " << beybladeName[index] << endl;
    cout << "Product Code: " << productCode[index] << endl;
    cout << "Type: " << type[index] << endl;
    cout << "Plus Mode: " << plusMode[index] << endl;
    cout << "System: " << system[index] << endl;
    cout << " " << endl;
  }
}
问题原因
  • 头文件引用错误:文件流需要引入<fstream>头文件,原有代码写为<stream>会导致编译失败。
  • 数组作用域隔离:main函数和readCards函数中分别定义了同名的局部数组,readCards读取的数据全部存在函数内部的局部数组中,函数运行结束后局部数组就会被销毁,完全没有写入main中定义的数组。
  • 变量名大小写错误:读取循环中使用了大写I作为数组下标,和定义的循环变量小写i不是同一个变量,会触发未定义行为/编译错误。
  • 函数无传参:原readCards函数没有定义参数,无法获取main中数组的内存地址,自然无法通过指针写入数据。
  • 缺少越界判断:读取循环没有校验下标i是否超过数组最大长度,若文件中数据行数超过数组容量,会触发内存越界错误。
修正后实现

修改函数签名,将主函数中数组的指针作为参数传入readCards,移除函数内部重复定义的局部数组,修复大小写错误和越界判断,修正后代码如下:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// 函数参数接收各数组的指针(数组传参自动退化为指针),同时传入数组最大长度
void readCards(int* id, string* beybladeName, string* productCode, string* type, string* plusMode, string* system, const int SIZE);

int main() {
  const int SIZE = 10;
  int id[SIZE];
  string beybladeName[SIZE];
  string productCode[SIZE];
  string type[SIZE];
  string plusMode[SIZE];
  string system[SIZE];

  // 传入数组首地址和长度,函数内部通过指针直接操作main中的数组内存
  readCards(id, beybladeName, productCode, type, plusMode, system, SIZE);

  // 验证读取结果
  for (int index = 0; index < SIZE; index++) {
    cout << "#:" << id[index] << endl;
    cout << "Beyblade Name: " << beybladeName[index] << endl;
    cout << "Product Code: " << productCode[index] << endl;
    cout << "Type: " << type[index] << endl;
    cout << "Plus Mode: " << plusMode[index] << endl;
    cout << "System: " << system[index] << endl;
    cout << " " << endl;
  }

  return 0;
}

void readCards(int* id, string* beybladeName, string* productCode, string* type, string* plusMode, string* system, const int SIZE) {
  ifstream inputFile;
  int i = 0;

  inputFile.open("cards.txt");
  cout << "Reading all cards information..." << endl;
  if (inputFile) {
    // 增加下标越界判断,避免内存溢出
    while (i < SIZE && inputFile >> id[i] >> beybladeName[i] >> productCode[i] >> type[i] >> plusMode[i] >> system[i]) {
      i++;
    }
    cout << "Total read " << i << " cards information." << endl;
  } else {
    cout << "Failed to open cards.txt" << endl;
  }
  inputFile.close();
}

注意:如果卡牌名称、类型等字段存在带空格的内容,不能直接用>>运算符读取,需要改用getline按行读取后做字段拆分。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 18:18:51