【问题标题】:Text File Reading Issue in C++C++ 中的文本文件读取问题
【发布时间】:2012-10-23 22:30:20
【问题描述】:

我设计了一个基于链接列表的 BookStore,其中书籍的属性将存储在一个节点中等等。另外,在程序结束时,我必须将所有数据库保存到文本文件中(我尝试了二进制读取,但该死的我被杀了并且无法做到)然后重新加载每本书的所有信息一个接一个并将其存储在节点中并重新制作LinkList。 现在保存已完成,完全没有问题。但我在读取文本文件时遇到问题。

文件中的保存结构是:::

BookID(int) - BookName(string) - Author(string) - BookType(string) - Copies(long) - Price(long) - '\n'(转到下一行)

示例: 1 ObjectOrientedParadigm R.Lafore 编码 5 900 2 ObjectOrientedParadigm R.Lafore 编码 5 900 等等……

这里是保存功能。

bool BookStoreDataBase<mytype>::save_all_data()
{
    if(!is_Empty()) //if list is not empty
    {
        BOOK<mytype> *temp = head;   //created a copy of head
        ofstream file("database.txt", ios_base::app); //created file, to write at the end (append)
        while(temp != tail) //while list ends
        {
            file<<temp->ID<<' '<<temp->bookName<<' '<<temp->author<<' '<<temp->book_type<<' '<<temp->copies<<' '<<temp->price<<' ';  //write all info
            temp = temp->next; //move temp to next node
        }
        file<<temp->ID<<' '<<temp->bookName<<' '<<temp->author<<' '<<temp->book_type<<' '<<temp->copies<<' '<<temp->price<<' '; //for last book's info
        return true; //to confirm sucessfull writing
    }
    else //if list is empty
    {
        return false; //to confirm error in writing
    }
}

问题:: 当我开始阅读时,第一行被正确读取并存储在列表中,但是下一次,我不能让文件从下一行读取,因此是'\n'。 &这会产生问题。文件再次读取第一行并使用相同的数据创建第二个节点。

加载函数:

void BookStoreDataBase<mytype>::load_all_data()
{
    int ID;         //variable to store ID of a book
    string bookName;//string to store name of a book
    string author;  //string to store name of author of book
    string book_type;//string to store type of a book
    long copies;    //variable to store no. of copies a book
    long price;     //variable to store price of a book
    string status;  //to store status of a book, either its in stock or not


    ifstream file("database.txt");
    while(file) //I have tried file.eof but its not working, don't know why
    {
        file>>ID>>bookName>>author>>book_type>>copies>>price>>status; //read file

        BOOK<mytype> *temp = new BOOK<mytype>(0, 0, bookName, author, book_type, copies, price);  //create a new node in memory and save all the data

        if(is_Empty()) //if list is empty, then make 1st node
        {
            head = tail = temp;
        }
        else //other wise make the next node
        {
            tail->next = temp;
            temp->prev = tail;
            tail = temp;
        }
    }
}

更多 读数比实际记录少 1 倍。即如果 .txt 有 4 本书的记录,则创建 3 个节点,(每个节点中仅重复第 1 个的信息),而它应该读取并创建 4 个节点!

我是初学者,任何好的帮助将不胜感激。

【问题讨论】:

  • 在创建 BookStoreDataBase 和 Book 时,您将在 之间使用哪些类型?

标签: c++ file-io file-handling


【解决方案1】:

我建议您使用std::getline() 获取整行,然后使用stringstream 类将所有内容读入相应的变量。

【讨论】:

  • 把语法交给我。我已经试过了。只需阅读第一行就可以了 CODE::::::stringstream mystr; ifstream 文件(“database.txt”);而(文件>>ID>>书名>>作者>>book_type>>副本>>价格>>状态){ mystr>>ID>>书名>>作者>>book_type>>副本>>价格>>状态;
  • 只是一个建议@deep_ecstasy 你可以发布你想阅读的文本文件吗?我会给你一个正确的解决方案
  • 好的解决了。 1. 我使用了 stringstream mystr (#include) 2. 我试图读取错误的加载函数中的 bookstatus。 3. 更正 while 条件 -> while(file>>ID>>bookName>>author>>book_type>>copies>>price) 4. 进入 while,正确阅读 ->> mystr>>ID>>bookName>>author> >book_type>>份数>>价格;我的 Dev Bros 非常爱你。你做到了。非常感谢!
【解决方案2】:

while (!file.eof()) 错了,while (file) 错了。似乎每个新程序员都无法理解从文件中读取的正确方法。我希望我知道为什么,向新手提供建议会更容易。基本的误解似乎是新手认为您应该测试文件结束 first 然后阅读 second。什么时候你应该先阅读,然后再看看阅读是否失败。

这是从文件中读取的正确方法

while (file >> ID >> bookName >> author >> book_type >> copies >> price >> status)
{
}

试试看,看看还有什么问题。

我刚刚注意到另一个问题,您尝试读取您所说的状态是一个字符串,但是在您对文件格式的描述中没有状态。我认为这是你真正的问题。

【讨论】:

  • Bjarne Stroustrup 使用 while(!file.eof())
  • 显示上下文。使用eof 并不违法,只是大多数人用错了。
  • 这个while (!file.eof()) { file &gt;&gt; x; ...; } 是我们通常看到的并且是错误的,和这个file &gt;&gt; x; while (!file.eof()) { ...; file &gt;&gt; x; } 有很大的不同,虽然我仍然更喜欢我上面的版本;
  • 我已经完成了你的方法,它工作正常,BUTTTT 它的读取时间减少了 1 倍。如果文件有 3 条记录。它正在阅读 2 本书并创建 2 个节点。 :( :(
  • 你看过我提到的状态问题了吗?如果不是这样,请在此处发布文件的内容。没有看到你能看到的东西很难提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-01
  • 1970-01-01
  • 2013-11-20
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
相关资源
最近更新 更多