简体   繁体   中英

Reading and writing from a file

I need to make card catalog entries for a library system and will need a menu and save the card and read the card from a file. I'm just looking for help and not saving for anyone to do it just tips if you have any because I want ro learn and not just copy and paste.

I already finished the information they need to add but having a problem making them save and read from a file and choosing from the menu to add new entry, review entry.

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

struct libary
{
    string title;
    string author;
    string ISBN_code;
    int page_count;
    int publish_year;
} s[10];

bool processMenu()
{
    int choice, spot;
    cout << "Main Menu" << endl;
    cout << "Select your options" << endl;
    cout << "1. Add a new entry to the system" << endl;
    cout << "2. Review an entry" << endl;
    cout << "3. Save entries to a file" << endl;
    cout << "4. Read entries from a file" << endl;

    cin >> choice;
    switch (choice)
    {
        case 1:
            cout << "which spot do you want to add a entry to the system " << endl;
            cin >> spot;
            break;
        case 2:
            cout << "which spot do you want to review to a file " << endl;
            cin >> spot;
            break;
        case 3:
            cout << "Save entries to a file" << endl;
            ofstream myfile;
            myfile.open("file.txt");
            myfile << "this will show in the file";

            break;

        case 4:
            cout << "read entries from a file :" << endl;
            break;
        case 7:
            return 0;
    }
}
int main()
{

    while (!processMenu())
    {
        cout << "Sorry, that is not a valid choice." << endl;
        cout << "Please try again." << endl << endl << endl;
    }
    cout << " Enter information of each card catalog" << endl;

    for (int i = 0; i < 10; i++)
    {
        cout << endl;
        cout << " Enter title " << endl;
        cin >> s[i].title;

        cout << endl;

        cout << " Enter author " << endl;
        cin >> s[i].author;

        cout << endl;

        cout << " Enter Page count " << endl;
        cin >> s[i].page_count;

        cout << endl;

        cout << " Enter publish year " << endl;
        cin >> s[i].publish_year;

        cout << endl;

        cout << " Enter ISBN code, it should be 13 digits" << endl;
        cin >> s[i].ISBN_code;
        while (s[i].ISBN_code.length() != 13)
        {
            cout << " Please enter a ISBN code thats 13 digits" << endl;
            cin >> s[i].ISBN_code;
            cout << endl;
            cout << endl;
        }

    }

    cout << " Displaying Information" << endl;

    for (int i = 0; i < 10; i++)
    {
        cout << " title: " << s[i].title << endl;
        cout << " author: " << s[i].author << endl;
        cout << " page count: " << s[i].page_count << endl;
        cout << " publish year: " << s[i].publish_year << endl;
        cout << " ISBN code: " << s[i].ISBN_code << endl;
    }

    return 0;
}

It lets me pick a spot but won't add new entry nor review or save/read.

You can make your own type writable to a stream ( cout or ofstream ) and/or readable from a stream ( cin or ifstream ) just by overriding its output ( operator<< ) and/or input operator ( operator>> ).

#include <fstream> // ifstream, ofstream
#include <iostream> // cin, cout
#include <string> // getline, string

using namespace std;

struct Card
{
    // Writing to the standard output ('cout').
    friend ostream& operator<<(ostream& os, const Card& card);
    // Reading from the standard input ('cin').
    friend istream& operator>>(istream& is, Card& card);
    // Writing to a file ('ofstream').
    friend ofstream& operator<<(ofstream& os, const Card& card);
    // Reading from a file ('ifstream).
    friend ifstream& operator>>(ifstream& is, Card& card);

    string title = "";
    string author = "";
    string isbn = "";
    int page_count = -1;
    int publish_year = -1;
};

ostream& operator<<(ostream& os, const Card& card)
{
    os << "Title: " << card.title << '\n'
       << "Author: " << card.author << '\n'
       << "ISBN: " << card.isbn << '\n'
       << "Page count: " << card.page_count << '\n'
       << "Publish year: " << card.publish_year << '\n';
    return os;
}

istream& operator>>(istream& is, Card& card)
{
    // 'getline' needs for the reading of strings because the title of a book or
    // its author's name may contain Space (' ') characters. 'cin' and
    // 'ifstream' separate input by every whitespace. So, if we want to allow
    // spaces in our fields we have to separate them by a special delimiter 
    // character. We use the Newline ('\n') character here to separate fields.
    // `page_count` and `publish_year` are numbers, they don't contain 
    // whitespaces, so they simply can be read with 'operator>>'.
    cout << "Title: ";
    std::getline(is, card.title);
    cout << "Author: ";
    std::getline(is, card.author);
    cout << "ISBN code: ";
    std::getline(is, card.isbn);
    cout << "Page count: ";
    is >> card.page_count;
    cout << "Publish year: ";
    is >> card.publish_year;
    return is;
}

// The next two functions is for files.
ofstream& operator<<(ofstream& os, const Card& card)
{
    os << card.title << '\n'
       << card.author << '\n'
       << card.isbn << '\n'
       << card.page_count << '\n'
       << card.publish_year << '\n';
    return os;
}

ifstream& operator>>(ifstream& is, Card& card)
{
    std::getline(is, card.title);
    std::getline(is, card.author);
    std::getline(is, card.isbn);
    is >> card.page_count;
    is >> card.publish_year;
    return is;
}

int main()
{
    Card card;

    // Read the informations of a card from the standard input.
    cin >> card;

    // Write the informations of a card to the standard output.
    cout << card;

    // Write the informations of a card to "file.txt".
    ofstream ofs("file.txt");
    ofs << card;
    ofs.close();

    // Read the informations of a card from "file.txt".
    ifstream ifs("file.txt");
    ifs >> card;
    ifs.close();

    return 0;
}

Important: For the sake of simplicity, I omitted error checking at the opening of a file and at the writing/reading of a stream. In your code you should do these.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM