【问题标题】:Writing from an input file to an output file从输入文件写入输出文件
【发布时间】:2014-10-18 02:14:24
【问题描述】:

在这段代码中打开我的文本文件时遇到问题。我做的正确吗?这周刚开始使用 C++。

我现在无法写入输出文件。我得到的唯一输出是这个。 libc++abi.dylib:以 std::invalid_argument 类型的未捕获异常终止:stoi:无转换 (lldb)

提前谢谢各位。

这是我的employeesIn.txt

<123>,<John>,<Brown>,<125 Prarie Street>,<Staunton>,<IL>,<62088>
<124>,<Matt>,<Larson>,<126 Hudson Road>,<Edwardsville>,<IL>,<62025>
<125>,<Joe>,<Baratta>,<1542 Elizabeth Road>,<Highland>,<IL>,<62088>
<126>,<Kristin>,<Killebrew>,<123 Prewitt Drive>,<Alton>,<IL>,<62026>
<127>,<Tyrone>,<Meyer>,<street>,<999 Orchard Lane>,<Livingston>,<62088>

这是我的代码

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

using namespace std;

struct Person {
string first;
string last;

};

struct Address {
string street;
string city;
string state;
string zipcode;
};

struct Employee {
Person name;
Address homeAddress;
int eid;
};

void readEmployee(istream& in, Employee& e);
void displayEmployee(ostream& out, const Employee& e);

int main(int argc, const char * argv[])
{
Employee e[50];

ifstream fin;
ofstream fout;

fin.open("employeesIn.txt");

if (!fin.is_open()) {
    cerr << "Error opening employeesIn.txt for reading." << endl;
    exit(1);
}
fout.open("employeesOut.txt");
if (!fout.is_open()) {
    cerr << "Error opening employeesOut.txt for writing." << endl;
    exit(1);
}
int EmployeePopulation = 0;
readEmployee(fin, e[EmployeePopulation]);
while (!fin.eof()) {
    EmployeePopulation++;
    readEmployee(fin, e[EmployeePopulation]);
}
fin.close();
for (int i = 0; i <= EmployeePopulation - 1; i++) {
    displayEmployee(fout, e[i]);
}

fout.close();

cout << endl;

return 0;
}

void readEmployee(istream& in, Employee& e)
{
string eidText;
if ( getline(in, eidText, ',') ) {
    e.eid = stoi(eidText);

    getline(in, e.name.first, ',');
    getline(in, e.name.last, ',');

    getline(in, e.homeAddress.street, ',');
    getline(in, e.homeAddress.city, ',');
    getline(in, e.homeAddress.state, ',');

    string zipcodeText;
    getline(in, zipcodeText, ',');
    e.homeAddress.zipcode = stoi(zipcodeText);
}


}
void displayEmployee(ostream& out, const Employee& e)
{
out << "Customer Record: " << e.eid
<< endl
<< "Name: " << e.name.first << " " << e.name.last
<< endl
<< "Home address: " << e.homeAddress.street
<< endl
<< e.homeAddress.city << ", " << e.homeAddress.state << " " << e.homeAddress.zipcode
<< endl
<< endl
<< endl;
}

【问题讨论】:

  • 您的错误消息说明了问题所在。您的程序无法打开给定文件。检查文件“employeesIn.txt”和您的二进制文件是否在同一目录中。
  • 我在两个文件夹中都有它,所以我不确定出了什么问题。
  • 好吧,我想通了,现在的问题是它为什么不把它写到我的employeesOut.txt 文件中?我没有错误。除此之外,在我的输出框中。 libc++abi.dylib:以 std::invalid_argument 类型的未捕获异常终止:stoi:无转换(lldb)
  • 使用中间临时变量,关闭所有优化,进行调试构建,使用调试器进行断点/步进/检查。修复发现的问题。

标签: c++ visual-c++ file-io iostream


【解决方案1】:

这正是它所说的:stoi failed。

你只有两次,所以很明显你的eidText不是数字,或者你的邮政编码不是数字。

我们不知道这是你的输入文件有问题还是你的解析有问题,所以在你stoi之前输出eidTextzipcodeText到控制台,这样你就可以看到它们的值是,然后相应地继续您的诊断。

【讨论】:

  • 我已经更改了我的原始帖子以包含我的 employeesIn.txt 文件的样子。我需要从字符串邮政编码更改我的邮政编码声明吗?转换邮政编码; ?
  • 所以只要把上面写着 fout 的地方改成 cout 就可以看到控制台中的值了?
  • @MatthewTingle No.... 你在用哪本书?现在您已经添加了输入,很清楚发生了什么。您将文本"&lt;62025&gt;" 读入zipcodeText,然后使用std::stoi 将其转换为整数。但&lt;62025&gt; 不是整数。你需要以某种方式处理那些&lt;/&gt;。只需使用临时代码行将zipcodeText 写入控制台即可轻松向您揭示这一点!
  • 所以把 zipcodeText 输出到控制台,这样我就可以看到了。我只是写
  • C++ 编程,包括数据结构的程序设计 D.S. Malik 第六版
猜你喜欢
  • 2016-07-13
  • 2021-09-25
  • 1970-01-01
  • 1970-01-01
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-24
相关资源
最近更新 更多