Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在这里得到一个错误,但不确定它是什么_C++_Visual C++_File Io_Inputstream_Outputstream - Fatal编程技术网

C++ 在这里得到一个错误,但不确定它是什么

C++ 在这里得到一个错误,但不确定它是什么,c++,visual-c++,file-io,inputstream,outputstream,C++,Visual C++,File Io,Inputstream,Outputstream,我的构建在这里是完全成功的,但是没有输出到我的文本文件,我知道几天前我问了一个关于这个程序的问题,后来我改变了它。我现在做错了什么 提前谢谢各位。 我正在尝试从employeesIn.txt文件输入,并创建一个由employee结构组成的employeesOut.txt文件 这是我的文本文件 123,John,Brown,125 Prarie Street,Staunton,IL,62088 124,Matt,Larson,126 Hudson Road,Edwardsville,IL,6202

我的构建在这里是完全成功的,但是没有输出到我的文本文件,我知道几天前我问了一个关于这个程序的问题,后来我改变了它。我现在做错了什么

提前谢谢各位。 我正在尝试从employeesIn.txt文件输入,并创建一个由employee结构组成的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
输出应该如下所示

员工记录:123 姓名:约翰·布朗 家庭住址:普拉里街125号 伊利诺伊州斯汤顿62088

员工记录:124 我叫马特·拉森 家庭住址:。。。。等等

这是我的密码

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

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;
};

int readEmployee(istream& in, Employee eArray[]);
void displayEmployee(ostream& out,Employee eArray[], int EmployeePopulation);
const int arr=50;
Employee eArray[arr];

ifstream fin;
ofstream fout;

int main(int argc, const char * argv[])
{   
    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 tingle = readEmployee(fin, eArray);

    fin.close();
    displayEmployee(fout, eArray, tingle);        
    fout.close();

    exit(0);
}

int readEmployee(istream& in, Employee eArray[])
{
    string eidText;
    string line;
    getline(in, line);

    int EmployeePopulation = 0;
    while (!in.eof()) {    
        getline(in, eidText, ',');
        eArray[EmployeePopulation].eid = stoi(eidText);
        getline(in, eArray[EmployeePopulation].name.first, ',');
        getline(in, eArray[EmployeePopulation].name.last, ',');
        getline(in, eArray[EmployeePopulation].homeAddress.street, ',');
        getline(in, eArray[EmployeePopulation].homeAddress.city, ',');
        getline(in, eArray[EmployeePopulation].homeAddress.state, ',');
        getline(in, eArray[EmployeePopulation].homeAddress.zipcode);
        EmployeePopulation++;        
    }
    return EmployeePopulation;    
}

void displayEmployee(ostream& out, Employee eArray[], int EmployeePopulation)
{
    for (int i = 0; i <= EmployeePopulation - 1; i++) {
        out << "Employee Record: " << eArray[i].eid
            << endl
            << "Name: " << eArray[i].name.first << " " << eArray[i].name.last
            << endl
            << "Home address: " << eArray[i].homeAddress.street
            << endl
            << eArray[i].homeAddress.city << ", " << eArray[i].homeAddress.state << " " <<  eArray[i].homeAddress.zipcode
            << endl
            << endl;
    }
}
#包括
#包括
#包括
#包括
使用名称空间std;
结构人{
先串;
最后一串;
};
结构地址{
弦街;;
字符串城市;
字符串状态;
字符串zipcode;
};
结构雇员{
人名;
地址家庭地址;
int eid;
};
int readEmployee(istream&in,员工耳环[]);
void display Employee(ostream&out,雇员耳牌[],国际雇员人数);
常数int arr=50;
雇员耳环[arr];
流鳍;
流式流量计;
int main(int argc,const char*argv[]
{   
财务公开(“employeesIn.txt”);
如果(!fin.is_open()){

cerr在您的
readEmployee
函数中,您传递了
isstream&In
。我想您应该检查
while(!In.eof())
ans not
while(!fin.eof())
。 你的
getline(fin,line);
也应该是
getline(in,line);

两件事:

您应该使用主菜单末尾的
return0
而不是
exit(0)

执行多次读取并尝试转换数据后检查
eof
是错误的。您需要检查读取本身是否失败

这纠正了
eof
问题。我的程序正在崩溃,因为
stoi
在读取失败时引发了异常

int readEmployee(istream& in, Employee eArray[])
{
    string eidText;
    string line;
    //This discards the first line.  Incorrect for the test data you supplied.
    getline(in, line);

    int EmployeePopulation = 0;
    //Check for errors while reading, not eof after the fact.
    //This was crashing because stoi failed when no data was
    //read due to eof being true after the loop check.
    while(  getline(in, eidText, ',') &&
            getline(in, eArray[EmployeePopulation].name.first, ',') &&
            getline(in, eArray[EmployeePopulation].name.last, ',') &&
            getline(in, eArray[EmployeePopulation].homeAddress.street, ',') &&
            getline(in, eArray[EmployeePopulation].homeAddress.city, ',') && 
            getline(in, eArray[EmployeePopulation].homeAddress.state, ',') &&
            getline(in, eArray[EmployeePopulation].homeAddress.zipcode))
    {
        eArray[EmployeePopulation].eid = stoi(eidText);
        EmployeePopulation++;
    }
    return EmployeePopulation;
}

如果使用
员工的
向量

您可以通过引用函数来传递它。
这些函数可以通过使用
std::vector::size()
获取员工人数。 使用
push_back
方法时,
std::vector
会自动展开

如果为类创建了输入和输出方法,则不必违反封装:

class Person // using class to support privacy and encapsulation
{
    std::string first_name;
    std::string last_name;
  public:
    friend std::istream& operator>>(std::istream& inp, Person& p);
    friend std::ostream& operator<<(std::ostream& out, const Person& p);
};

std::istream& operator>>(std::istream& inp, Person& p)
{
  std::getline(inp, p.first_name, ',');
  std::getline(inp, p.last_name, ',');
}

std:ostream& operator<<(std::ostream& out, const Peron& p)
{
  out << "Name: ";
  out << p.first_name;
  out << " ";
  out << p.last_name;
}

class Employee
{
  Person name;
  Address addr;
  public:
    friend std::istream& operator>>(std::istream& inp, Employee& e);
};

std::istream& operator>>(std::istream& inp, Employee& e)
{
  inp >> name;
  inp >> addr;
};
class Person//使用类支持隐私和封装
{
std::string first_name;
std::字符串last_name;
公众:
friend std::istream&operator>>(std::istream&inp、Person&p);
friend std::ostream&operator(std::istream&inp,Person&p)
{
std::getline(inp,p.first_name,',');
std::getline(inp,p.last_name,',');
}
std:ostream和操作员姓名;
inp>>地址;
};

缺少的格式化输入和输出留给读者作为练习。

确切的错误是什么?我没有收到错误,只是没有像我想的那样输出到文件中。那么你的标题很容易引起误解。你是否在调试器中仔细检查过程序?你确定正在读取任何内容吗?
while(!fin.eof()))
几乎总是错误的,您应该检查实际读取的返回值,不希望在出现问题后再发现问题。文本文件中的最后一行
127,Tyrone,Meyer,street,999 Orchard Lane,Livingston,62088
与其他行的格式不匹配。预期的输出是什么?实际输出是什么?我们没有做任何更改hese更新了上述代码,输出仍然为空:/