【问题标题】:Program not writing to file like I thought it would程序没有像我想象的那样写入文件
【发布时间】:2014-09-05 15:47:42
【问题描述】:

我以为我已经完全解决了这个问题,但实际上并没有向文件写入任何内容。它正在打开 employeesOut.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

libc++abi.dylib:以 std::invalid_argument 类型的未捕获异常终止:stoi:无转换 (lldb)

我相信错误在我的 main.cpp 中,所以就是这样。

#include <iostream>
#include <string>
#include <fstream>
#include "Employee.h"

using namespace std;

bool openFileForReading(ifstream& fin, const string& filename);
bool openFileForWriting(ofstream& fout, const string& filename);

int readFromFile(ifstream& in, Employee empArray[]);

void writeToFile(ofstream& out, const Employee empArray[], const int numberofEmployees);

int main(){

ifstream fin;
ofstream fout;

if(!openFileForReading(fin, "employeesIn.txt")) {
    cerr << "Error opening employeesIn.txt for reading." << endl;
    exit(1);
}

if(!openFileForWriting(fout, "employeesOut.txt")) {
    cerr << "Error opeing employeesOut.txt for writing." << endl;
    exit(1);
}

Employee employeeArray[50];

int employeeCount = readFromFile(fin, employeeArray);

fin.close();

writeToFile(fout, employeeArray, employeeCount);

fout.close();

cout << "Program successful." << endl << endl;


return 0;
}

bool openFileForReading(ifstream& fin, const string& filename) {
fin.open("employeesIn.txt");

return (fin.is_open());

}

bool openFileForWriting(ofstream& fout, const string& filename) {
fout.open("employeesOut.txt");

return (fout.is_open());

}

int readFromFile(ifstream& in, Employee empArray[]) {
int temp = 0;
string eidText;
string first;
string last;
string street;
string city;
string state;
string zipcode;

while(!in.eof()) {
    getline(in, eidText, ',');
    getline(in, first, ',');
    getline(in, last, ',');
    getline(in, street, ',');
    getline(in, city, ',');
    getline(in, state, ',');
    getline(in, zipcode, ',');

    empArray[temp].setEid(stoi(eidText));
    empArray[temp].setName(first, last);
    empArray[temp].setAddress(street, city, state, zipcode);

    temp++;
}

return temp;
}

void writeToFile(ofstream& out, const Employee empArray[], const int numberOfEmployees) {

for (int i = 0; i < numberOfEmployees; i++){
    out << "Employee Record: " << empArray[i].getEid()
    << endl
    << "Name: " << empArray[i].getName()
    << endl
    << "Home Address: " << empArray[i].getAddress() 
    << endl
    << endl;
}

}

【问题讨论】:

  • 你能发布你作为输入的文件的前几行吗?
  • 为什么要使用自己的函数打开文件而不是直接使用流方法?看起来有点浪费功能。
  • 你的文件打开函数没有使用它们的参数,所以去掉参数。
  • 当您使用调试器时,哪个语句导致了问题?您是否在写入前尝试打印数据?
  • 我已将输入文件添加到上述帖子中。

标签: c++ file-io output ostream


【解决方案1】:

通过将getline(in, zipcode, ','); 行更改为getline(in, zipcode, '\n'); 来解决您最初的问题,因为这将使getline 在换行符处结束。当stoi 被指定为“Matt”作为参数时,会产生错误。

但是,您的 readFromFile 函数正在接收您的数组的副本,因此它所做的更改不会移回您的 main() 函数(因为您返回的是计数,而不是数组)。

由于传递引用数组很麻烦,请尝试使用 std::vector(在 #includeing &lt;vector&gt; 之后)对您的个别员工进行分组。如果Employee.H 指定了无参数构造函数或没有构造函数(并且您的编译器有一个默认构造函数),那么

std::vector<Employee> employeeVector;
void readFromFile(ifstream& in, std::vector<Employee> &empVec);

void readFromFile(ifstream& in, std::vector<Employee> &empVec) {
// int temp = 0; unneeded.
string eidText;
...
string zipcode;

while(!in.eof()) {
    getline(in, eidText, ',');
    ...
    getline(in, zipcode, '\n');

    Employee tempEmp();
    tempEmp.setEid(stoi(eidText));
    ...
    empVec.push_back(tempEmp);

    // temp++; no longer need this
    }
} 

可能是更好的选择。即使构造函数非常复杂,它仍然可能比引用数组更简单(尽管您必须更改Employee tempEmp(); 行。顺便说一句,如果这抱怨并且无法编译,我可能会遇到 MVP 错误- 删除 ()s,你会没事的。

编辑: 由于您已被指示使用Employees 的数组,因此您可以根据需要选择返回该数组,或者通过指针传递它应该是合法的。这将要求您通过以与原始代码类似的方式沿数组移动指针来进行迭代。我已经离开了上面的vector 方法,因为我认为它更干净,但是由于您不能使用它,您可以从两种选择中进行选择(返回数组,传递指针而不是对象)。我猜你应该选择指针,因为现在这似乎是一个家庭作业式的问题。

在这种情况下,您可以使用

int main() {
...
Employee* empPtr = employeeArray;
...
}

void readFromFile(ifstream& in, Employee* empPtr) {
    string eidText;
    ...
    string zipcode;

    while(!in.eof()) {
        getline(in, eidText, ',');
        ...
        getline(in, zipcode, '\n');

        (*empPtr).setEid(stoi(eidText));
        ...

        empPtr++;
        }
    } 

【讨论】:

  • 我需要逗号来解析我的输入文件,我已经添加了上面的文件。当我尝试通过引用传递时,我只会得到错误:/
  • 如果您删除逗号(仅从邮政编码,而不是从其他),getline 将在换行符处停止。引用传递会给您带来哪些错误?
  • 我将 empArray 声明为 'Employee &' 类型的引用数组
  • 我在这一行也收到错误 int employeeCount = readFromFile(fin, employeeArray);调用“readFromFile”没有匹配的函数
  • 我的问题可能出在这一行 empArray[temp].setEid(stoi(eidText));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 1970-01-01
  • 2011-07-05
相关资源
最近更新 更多