将记录添加到文件并读回

时间:2019-03-01 11:18:49

标签: c++ file c++11

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>


using namespace std;

class Book
{
    public:
        char ISBN [5];
        char Title [20];
        char authorName [20];
        char Price [10];
        char Year [10];
        char NumOfPages [10];
        char delimiter = ',';
};


void AddBook ()
{
    fstream file;
    file.open("Records.txt", ios::out|ios::app);
    Book b;
    cout << "Enter ISBN: " << endl;
    cin>>b.ISBN;
    cin.ignore();
    cout << "Enter Title: " << endl;
    cin.getline(b.Title,sizeof(b.Title));
    cout << "Enter Author's Name: " << endl;
    cin.getline(b.authorName , sizeof(b.authorName));
    cout << "Enter Price: " << endl;
    cin.getline(b.Price,sizeof(b.Price));
    cout << "Enter Year: " << endl;
    cin.getline(b.Year,10);
    cout << "Enter Number Of Pages: " << endl;
    cin.getline(b.NumOfPages , sizeof(b.NumOfPages));
    cin.ignore();
    file.write(reinterpret_cast<char*>(&b), sizeof(b));
    file.write(reinterpret_cast<char*>(&b.delimiter), sizeof(b.delimiter));
    file.close();

}


void DeleteBook ()
{

}
void UpdateBook ()
{

}
void PrintBook ()
{

}
void PrintAll ()
{
    ifstream file;
    file.open("Records.txt", ios::in);
    Book b;
    while (!file.eof())
    {
        cout << "ISBN :" << b.ISBN <<endl;
        cout << "Title :" << b.Title <<endl;
        cout << "Author's Name :" << b.authorName <<endl;
        cout << "Price :" << b.Price <<endl;
        cout << "Year :" << b.Year <<endl;
        cout << "Number of Pages "<< b.NumOfPages <<endl;
        file.read(reinterpret_cast <char*> (&b), sizeof(b));
    }
    file.close();
}

int main()
{
    int choice;
    do
    {
        cout << "The Menu for Book Store" << endl;
        cout << "1. Add Book: " << endl;
        cout << "2. Delete Book: " << endl;
        cout << "3. Update Book:" << endl;
        cout << "4. print a Book:" <<endl;
        cout << "5. print all Books " << endl;
        cout << "6. Exit the program "<<endl;
        cout << "Enter your choice here "<<endl;
        cin >> choice;

        switch (choice)
        {
            case 1:
                AddBook();
                break;
            case 2:
                DeleteBook();
                break;
            case 3:
                UpdateBook();
                break;
            case 4:
                PrintBook();
                break;
            case 5:
                PrintAll();
                break;
            default:
                cout << "Invalid Selection" << endl;
        }
    }
    while
        (choice != 6);

    return 0;
}

输出在文件中看起来很奇怪,并且输出正以奇怪的字符形式输出两次,另一个很好,但是书名附有ISBN,我需要一个解决方案,请问我如何解决它,因为不明显,这里的逻辑错误在哪里对我来说

输出

 ISBN :
 ╞3╧v`≡o F╙v└²a
Title :
≡o F╙v└²a
Author's Name 
:
Price :
F╙
Year :
oÇ≡o╘≡o   
 
Number of Pages   
 
ISBN :12345Jungle House
Title :Jungle House
Author's Name :ASad asad
Price :240
Year :2019
Number of Pages 300

1 个答案:

答案 0 :(得分:0)

您显示的代码有很多问题,但是看似垃圾输出的原因是您的printAll函数在Book对象中打印了数据{ {1}} 之前,您在其中阅读了任何内容。

这意味着对象b尚未初始化,并且将包含不确定的数据(可能看起来像是随机的或垃圾)。以任何方式使用此类值都会产生undefined behavior