C++ FileIO to read and write into objects variables

I'm trying to make a small library system where the user can add new book details(name, author & price). When implementing the FileIO system to read from a file the details of every book using the getline functions it becomes harder to separate my variables when I try to store them in temporary variables.

Ex:

"No Is a four letter word,Chris Jericho,17.67,"

"Harry Potter,JK Rowling,23.98,"

PS:Is there a better solution than adding the comma?

I tried to add a ',' character to separate every my strings but I need a better and more efficient solution that works with the getLine function.

int main(){
vector<Book> library;
//----Loading File Data_START
string line;
int nbArg=0;

string tempName, tempAuthor, tempPrice;


ifstream myfileL("List.txt");
if (myfileL.is_open())
{
    while (getline(myfileL, line))
    {
        tempPrice=tempAuthor = tempName = "";
        for (int j = 0; j < line.size(); j++){

            if (line.at(j) == ','){
                nbArg++;


                }
            else{
                switch (nbArg){
                case 0:
                    tempName += (line.at(j));
                    break;
                case 1:
                    tempPrice += (line.at(j));
                    break;
                case 2:
                    tempAuthor += (line.at(j));

                    break;

            }
            }
        }

        cout << tempName << endl << tempAuthor << endl << tempPrice << endl;
        cout << "End of Line"<< endl;
        nbArg = 0;
    }
    cout << "---------------------------" << endl;
    myfileL.close();
}

else cout << "Unable to open file";

//----Loading File Data_END
char inputKey = 's';
cout << "-----------------WELCOME----------------" << endl;
while (inputKey != 'q')
{
    cout << "---------------------------------------" << endl;
    cout << "Click \"1\" to add a book to your library" << endl;
    cout << "Click \"2\" to show how the number of books your possess" << endl;
    cout << "Click \"3\" to show details about your books" << endl;
    cout << "Click \"q\" to quit" << endl;
    cin >> inputKey;

    switch (inputKey)
    {
    case '1':
        addElem(library);
        break;

    case '2':
        cout << "You now own " << libSize(library) << " books !" << endl;
        break;

    case '3':
        showDetails(library);
        break;

    case 'q':
        cout << "GOODBYE!" << endl;
        Sleep(2000);
        break;
    }
}

return 0;

}

Upvotes: 0

Views: 59

Answers (2)

yuko
yuko

Reputation: 325

Here is a solution on a smooth way to load the library. You can let the BookList.txt file be delimited by TABs \t between Name, Author and Price instead of a comma , and then use getline() to separate with a TAB as in my example below.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

const char TAB = '\t';

struct StructBook {
    string Name, Author;
    double Price;
};

vector<StructBook> Library;

void GetBookDetails(string LineBookDetails, StructBook & Book) {
    string string_Price;
    stringstream StringStreamBookDetails(LineBookDetails);
    getline(StringStreamBookDetails, Book.Name, TAB);
    getline(StringStreamBookDetails, Book.Author, TAB);
    getline(StringStreamBookDetails, string_Price, TAB);
    stringstream(string_Price) >> Book.Price;
}

bool LoadLibrary() {
    ifstream FileBookList("BookList.txt");
    if(FileBookList.is_open()) {
        string LineBookDetails;
        StructBook Book;

        while(getline(FileBookList, LineBookDetails)) {
            GetBookDetails(LineBookDetails, Book);
            Library.push_back(Book);
            cout << Book.Name << ", " << Book.Author << ", " << Book.Price << endl;
        }

        FileBookList.close();
        return true;
    } else {
        return false;
    }
}

int main(){
    if(!LoadLibrary())
        cout << "Error: Unable to load library";

    //Rest of your program goes here

    return 0;
}

Your BookList.txt should look like this:

No Is a four letter word    Chris Jericho   17.67
Harry Potter    JK Rowling  23.98

Upvotes: 1

Radosław Cybulski
Radosław Cybulski

Reputation: 2992

Use specialized file format, which can contain structure (for example json, xml). This will prevent many, many problems.

If you can't, then add a separator character (just like you did), but pick one, that has least chances to occur in real strings. So for example end of line (every variable would go on it's own line) or \0 or \t.

Upvotes: 1

Related Questions