LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-18-2009, 12:53 AM   #1
fugatejeffrey
LQ Newbie
 
Registered: Feb 2009
Posts: 2

Rep: Reputation: 0
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.
 
Old 02-18-2009, 12:56 AM   #2
fugatejeffrey
LQ Newbie
 
Registered: Feb 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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";
	
	}	
}
 
Old 02-18-2009, 06:16 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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);
  ...
}
 
  


Reply

Tags
c++, file, getline, io, search



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GNU getline appears to choke with large file support (can't read >2GB) VelocideX Programming 5 06-06-2008 10:24 AM
searching a file nick2price Linux - Newbie 8 04-08-2008 06:32 PM
awk searching a string from a file within another file changcheh Linux - Software 7 12-29-2006 09:18 AM
File Searching gfrair Linux - Newbie 1 03-14-2005 06:24 PM
File Searching JC404 Linux - Newbie 2 08-02-2003 08:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration