I have a text file that represents a book , this first line of it for example is

P.G. Wodehouse, "Heavy Weather" (336 pp.) [PH.409 AVAILABLE FOR LENDING]

So there is authors name, title, number of pages and if the book is available. As they are not all seperayed by a comma how can I read this file and store them in appropriate arrays ?

If the pattern AA, "BB" (CC) [DD] stays the same, you could extract the strings AA BB CC and DD, by reading char by char and detecting when you are at a comma etc.

If the pattern holds ( as per @ddanbe )...

you might like to try parsing, using the power of C++ stringstream,
something like this ...

// getFileDataParsed.cpp //

/*
    I have a text file that represents a book ,
    this first line of it for example is:

    P.G. Wodehouse, "Heavy Weather 1" (336 pp.) [PH.409 AVAILABLE FOR LENDING]
    P.G. Wodehouse, "Heavy Weather 2" (336 pp.) [PH.409 AVAILABLE FOR LENDING]
    P.G. Wodehouse, "Heavy Weather 3" (336 pp.) [PH.411 AVAILABLE FOR LENDING]
    P.G. Wodehouse, "Heavy Weather 4" (336 pp.) [PH.411 AVAILABLE FOR LENDING]

    So there is:
    authors name,
    title,
    number of pages
    and if the book is available.

    As they are not all seperayed by a comma,
    how can I read this file and store them in appropriate arrays ?

*/

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;


const char* FILE_IN  = "books.txt";

// file line structure ...
// P.G. Wodehouse, "Heavy Weather" (336 pp.) [PH.409 AVAILABLE FOR LENDING]
struct Book
{
    string author;
    string title;
    string pages;
    string available;

    friend ostream& operator << ( ostream& os, const Book& b )
    {
        return os << b.author << ", " << b.title << ", "
                  << b.pages << ", " << b.available;
    }
} ;

bool takeInFileLine( istream& fin, Book& b )
{
    string line;
    if( getline( fin, line ) )
    {
        istringstream iss(line); // construct istringstream obj 'iss'
        getline( iss, b.author, ',' );

        string skip;
        getline( iss, skip, '"' );
        getline( iss, b.title, '"' );

        getline( iss, skip, '(' );
        getline( iss, b.pages, ')' );

        getline( iss, skip, '[' );
        getline( iss, b.available, ']' );

        return true;
    }
    // else ... if reach here ...
    return false;
}




int main()
{
    ifstream fin( FILE_IN );

    if( fin )
    {
        vector< Book > myBooks;
        Book tmp;

        while( takeInFileLine( fin, tmp ) ) // will read till end of file
        {
            myBooks.push_back( tmp );
        }
        fin.close();

        // show contents of vector ... //
        for( size_t i = 0; i < myBooks.size(); ++ i )
            cout << myBooks[i] << endl;

    }
    else
    {
        cout << "There was a problem opening file " << FILE_IN  << endl;
    }
}

Edited 1 Year Ago by David W

Comments
Nice.
This article has been dead for over six months. Start a new discussion instead.