使用文本文件中的数据初始化对象数组 当我试图在VisualC++ 2008 Express上编译下面的代码时,系统错误。我试图做的是用从文件中读取的数据初始化对象数组。我认为while循环内部有问题,因为当我在没有while循环的情况下手动初始化这些对象时,它似乎可以工作。以下是代码和文本文件: #include <iostream> #include <string> #include "Book.h" using namespace std; int main() { const int arraySize = 3; int indexOfArray = 0; Book bookList[arraySize]; double tempPrice;//temporary stores price string tempStr;//temporary stores author, title fstream fileIn( "books.txt" ); while ( !fileIn.eof( )) { getline(fileIn,tempStr); bookList[indexOfArray].setAuthor(tempStr); getline(fileIn,tempStr); bookList[indexOfArray].setTitle(tempStr); fileIn >> tempPrice; bookList[indexOfArray].setPrice(tempPrice); if ( indexOfArray < arraySize ) //shifting array index while not exceeding array size indexOfArray++; } fileIn.close(); return 0; } Author1 Book1 23.99 Author2 Book2 10.99 Autho3 Book3 14.56

使用文本文件中的数据初始化对象数组 当我试图在VisualC++ 2008 Express上编译下面的代码时,系统错误。我试图做的是用从文件中读取的数据初始化对象数组。我认为while循环内部有问题,因为当我在没有while循环的情况下手动初始化这些对象时,它似乎可以工作。以下是代码和文本文件: #include <iostream> #include <string> #include "Book.h" using namespace std; int main() { const int arraySize = 3; int indexOfArray = 0; Book bookList[arraySize]; double tempPrice;//temporary stores price string tempStr;//temporary stores author, title fstream fileIn( "books.txt" ); while ( !fileIn.eof( )) { getline(fileIn,tempStr); bookList[indexOfArray].setAuthor(tempStr); getline(fileIn,tempStr); bookList[indexOfArray].setTitle(tempStr); fileIn >> tempPrice; bookList[indexOfArray].setPrice(tempPrice); if ( indexOfArray < arraySize ) //shifting array index while not exceeding array size indexOfArray++; } fileIn.close(); return 0; } Author1 Book1 23.99 Author2 Book2 10.99 Autho3 Book3 14.56,c++,arrays,file,initialization,C++,Arrays,File,Initialization,密钥必须位于的内容中 #include "Book.h" 我复制粘贴了您的代码,并将#include替换为我对教科书的设想: class Book { std::string auth; std::string title; double price; public: void setAuthor(std::string& str) { auth = str; } void setTitle(std::strin

密钥必须位于的内容中

#include "Book.h"
我复制粘贴了您的代码,并将#include替换为我对教科书的设想:

class Book
{
    std::string auth;
    std::string title;
    double price;
public:
    void setAuthor(std::string& str)
    {
        auth = str;
    }

    void setTitle(std::string& t) 
    {
        title = t;
    }

    void setPrice(double d)
    {
        d = price;
    }
};

它被编译了。也许你可以分享你的书,或者在那里寻找任何问题?从书中的一些简单定义开始(如上所述),并开始阅读代码,直到找到导致问题的行。这是一种粗略的解决问题的方法,但有时却是最直接的方法。

关键必须在

#include "Book.h"
我复制粘贴了您的代码,并将#include替换为我对教科书的设想:

class Book
{
    std::string auth;
    std::string title;
    double price;
public:
    void setAuthor(std::string& str)
    {
        auth = str;
    }

    void setTitle(std::string& t) 
    {
        title = t;
    }

    void setPrice(double d)
    {
        d = price;
    }
};

它被编译了。也许你可以分享你的书,或者在那里寻找任何问题?从书中的一些简单定义开始(如上所述),并开始阅读代码,直到找到导致问题的行。这是一种解决问题的粗略方法,但有时却是最直接的方法。

看起来您正试图在循环中写入bookList[3]。您将循环三次填充数组,每次递增indexOfArray。这将把indexOfAray保留为3——您编写的条件将允许indexOfAray增加到3。然后,如果数据文件中的“14.56”后面有一个换行符,您将再次循环并尝试向bookList[indexOfArray].setAuthor()传递一个空字符串,因为indexOfArray已超过数组的末尾,因此导致segfault


我建议放弃硬编码数组,改用std::vector。在每个循环开始时,只需使用push_back()将一本新书添加到向量的末尾,然后使用back()访问数组中的新元素。

看起来您正在尝试写入循环中的bookList[3]。您将循环三次填充数组,每次递增indexOfArray。这将把indexOfAray保留为3——您编写的条件将允许indexOfAray增加到3。然后,如果数据文件中的“14.56”后面有一个换行符,您将再次循环并尝试向bookList[indexOfArray].setAuthor()传递一个空字符串,因为indexOfArray已超过数组的末尾,因此导致segfault


我建议放弃硬编码数组,改用std::vector。在每个循环开始时,只需使用push_back()将一本新书添加到向量的末尾,然后使用back()访问数组中的新元素。

代码中还有一个运行时错误:调用
fileIn>>tempPrice时,您没有读取整行内容。对
getline()
的下一次调用将读取到行尾,因此当您需要作者时,将得到一个空字符串

然后,在文本文件中取消一行,并尝试将标题转换为双行。这使得fstream信号出错,然后,您就有麻烦了

Brett是对的,带push_back的向量是更好的解决方案

Brett还正确地指出,如果您的文件有额外的行,您可能会遇到错误。您可以通过检查是否成功读取文件来修复此问题:

if(fileIn >> tempPrice)
{
    bookList[indexOfArray].setPrice(tempPrice);
}
else
{
    break;
}

if(!getline(fileIn,tempStr))
{
    break;
}

代码中还有另一个运行时错误:调用
fileIn>>tempPrice时没有读取整行代码。对
getline()
的下一次调用将读取到行尾,因此当您需要作者时,将得到一个空字符串

然后,在文本文件中取消一行,并尝试将标题转换为双行。这使得fstream信号出错,然后,您就有麻烦了

Brett是对的,带push_back的向量是更好的解决方案

Brett还正确地指出,如果您的文件有额外的行,您可能会遇到错误。您可以通过检查是否成功读取文件来修复此问题:

if(fileIn >> tempPrice)
{
    bookList[indexOfArray].setPrice(tempPrice);
}
else
{
    break;
}

if(!getline(fileIn,tempStr))
{
    break;
}

“系统错误”的确切内容是什么?“系统错误”的确切内容是什么?这很有趣,因为正如我所提到的,如果我手动初始化数组,它会工作。这很有趣,因为我所提到的,如果我手动初始化数组,它会工作。我重新排列了while循环并使用了fileIn.ignore()读取双精度后丢弃行尾。到目前为止,我们还没有学习向量,所以我试着坚持课堂上的内容。将来我一定会按照你的建议使用vector。我重新安排了while循环,并使用fileIn.ignore()在读取double后丢弃行尾。到目前为止,我们还没有学习向量,所以我试着坚持课堂上的内容。将来我一定会按照你的建议使用vector。