LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++, getline, searching through file issue (https://www.linuxquestions.org/questions/programming-9/c-getline-searching-through-file-issue-705557/)

fugatejeffrey 02-18-2009 12:53 AM

C++, getline, searching through file issue
 
Hello World!!!

ok I am taking a my first C++ class and we are suppose to write a very simple program. I have worked my way up to writing to a file, listing the contents of the file, and now i am on to the fun part of searching through the file for a particular word/string.

the program i have made compiles nicely, however, the search function does not provide me with the information i want it to.
I am going to give the portion of code that is giving me the problem. Any help would be appreciated.
Code:

void Book::search()
{
        ofstream inBookFile("book.txt",ios::in);
        string titlesearch;
        string author;
        string date;
        string desc;
        string title;

        if (inBookFile.is_open())
        {
          cout << "enter the title of a book you wish to search for: \n";
          cin >> titlesearch;
          while (! inBookFile.eof())
            {
                stringstream stream(title);
                getline(stream,title,',');
                getline(stream,author,',');
                getline(stream,date,',');
                getline(stream,desc,'.');
                if (titlesearch == title)
                {               
                        cout << "Title :\t\t" << title << endl;
                        cout << "Author :\t\t" << author << endl;
                        cout << "Publish Date :\t" << date << endl;
                        cout << "Description :\t" << desc << endl;
                }
          }
        }
        else cout << "Unable to open file";
}

BTW. this is a book keeping program. the file it is looking at follows, ordered as title, author, date, description (ignore the garbled nonsense as i was testing the program out)

Code:

red,        john,        a,        a.
red,        chris,        a,        a.
red,        turn,        a,        a.
red,        clown,        s,        s.
blue,        john,        3,        3.
df,        d,        d,        d.
john,        red,        blue,        green.


fugatejeffrey 02-18-2009 12:56 AM

P.S. i dont know if i was correct using the stringstream stream() function. but it worked previously for listing all of the books in the file. as shown below

Code:

void Book::display()
{
        ifstream inBookFile("book.txt",ios::in);
        string title;
        string author;
        string date;
        string desc;

        while (getline(inBookFile,title))
        {
                stringstream stream(title);
                getline(stream,title,',');
                getline(stream,author,',');
                getline(stream,date,',');
                getline(stream,desc,'.');
                cout <<setw(20)<< title << setw(23) << author << setw(20)<< left << date << "\n";
       
        }       
}


dwhitney67 02-18-2009 06:16 AM

There's a couple of problems with the code you posted concerning Book::search().

First, the following line:
Code:

ofstream inBookFile("book.txt",ios::in);
should be:
Code:

ifstream inBookFile("book.txt",ios::in);
Note the 'i'. If you want to avoid this mistake in the future, just declare an fstream (don't specify 'o' or 'i').

The next problem is quite simple; you are not reading from the file! I personally do not like checking for eof() because the eof-flag is not set until a read fails. Thus I recommend that you consider setting up your while loop as follows:
Code:

...

string data;

while (getline(inBookFile, data))
{
  stringstream stream(data);
  ...
}



All times are GMT -5. The time now is 03:31 PM.