ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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";
}
}
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.