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 ?

Recommended Answers

All 2 Replies

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;
    }
}
commented: Nice. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.