Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.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++_Segmentation Fault_Fstream_Binaryfiles - Fatal编程技术网

C++ 对分段错误的错误理解

C++ 对分段错误的错误理解,c++,segmentation-fault,fstream,binaryfiles,C++,Segmentation Fault,Fstream,Binaryfiles,我遇到了一个分段错误,从我读到的内容来看,这是因为我试图访问一些不允许访问的内存部分 我几乎可以肯定我的问题出现在showcounts函数的while循环中 while (input.read((char *) &account, sizeof(BankAccount))) 在多次尝试理解这个对read的调用是如何工作的之后,我认为我没有正确地理解它 我已经创建了3个BankAccount类型的帐户,它们存储在二进制文件AccountDetails.dat中,但是我无法访问它们 如果你

我遇到了一个分段错误,从我读到的内容来看,这是因为我试图访问一些不允许访问的内存部分

我几乎可以肯定我的问题出现在showcounts函数的while循环中

while (input.read((char *) &account, sizeof(BankAccount)))
在多次尝试理解这个对read的调用是如何工作的之后,我认为我没有正确地理解它

我已经创建了3个BankAccount类型的帐户,它们存储在二进制文件AccountDetails.dat中,但是我无法访问它们

如果你不能帮助我解释为什么我会收到这个分段错误,也许你可以解释一下read函数是如何工作的,它试图做什么,然后我可以做更多的评估?欢迎所有回复

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

class BankAccount
{
public:
    void CreateAccount();
    void ShowAccount();
private:
    int accountNumber;
    string firstName;
    string lastName;
    char accountType;
    double balance;
};

void MakeAccount();
void ShowAccounts();

int main()
{
    ShowAccounts();

    return 0;
}

void BankAccount::CreateAccount()
{
    cout << "Enter the account number.\n";
    cin >> accountNumber;
    cout << "Enter the first and last name of the account holder.\n";
    cin >> firstName >> lastName;
    cout << "What kind of account is it? S for savings or C for checking.\n";
    cin >> accountType;
    cout << "How much are you depositing into the account?\n";
    cin >> balance;
    cout << "CREATED\n";
}

void BankAccount::ShowAccount()
{
    cout << "Account Number:  " << accountNumber << endl;
    cout << "Name:  " << firstName << " " << lastName << endl;
    cout << "Account Type:  " << accountType << endl;
    cout << "Current Balance:  $" << balance << endl;
}

void MakeAccount()
{
    BankAccount account;
    ofstream output;
    output.open("AccountDetails.dat", ios::binary|ios::app);
    if (!output)
        cerr << "Failed to open file.\n";
    account.CreateAccount();
    output.write((char *) &account, sizeof(BankAccount));
    output.close();
}

void ShowAccounts()
{
    BankAccount account;
    ifstream input;
    input.open("AccountDetails.dat", ios::binary);
    if (!input)
        cerr << "Failed to open file.\n";
    while (input.read((char *) &account, sizeof(BankAccount)))
    {
        account.ShowAccount();
    }
    input.close();
}
#包括
#包括
使用名称空间std;
类别银行帐户
{
公众:
void CreateAccount();
void showcount();
私人:
国际帐户号码;
字符串名;
字符串lastName;
字符类型;
双平衡;
};
作废MakeAccount();
作废帐户();
int main()
{
showcounts();
返回0;
}
作废银行帐户::CreateCount()
{
cout>帐号;
cout>firstName>>lastName;
cout>accountType;
平衡;

cout当您试图将输入流中的字节直接读取到包含指针或字符串的结构上时,您将把它们包含的指针设置为垃圾(字符数组可以工作)尝试使用指针和字符串将导致未定义的行为,可能会导致出现分段错误。或其他任何情况

要检查这一点,请在调试器中加载程序,在该行上设置断点,然后运行。到达该行后,请检查结构及其成员,以查看它们包含的数据是否有效


尝试调整您的
BankAccount::CreateAccount()
功能是非交互式的。

您需要阅读-向文件中读取/写入数据的正确方法。您不能只将
std::string
写入文件。可能的重复项不可能正确地
读取
写入
包含std::string成员的对象(或std::vector或std::list或任何类型的指针或虚拟函数)。您必须想出一种存储此类对象的方法,而不涉及
读取
可以)。