Reading Specific Lines

reading file and picking out lines with a certain word or phrase

Page 1 of 1

7 Replies - 782 Views - Last Post: 02 April 2009 - 08:48 PM Rate Topic: -----

#1 xafltexans  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 19-March 09

Reading Specific Lines

Post icon  Posted 02 April 2009 - 07:55 PM

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main()
{
		//set variables
		string search, file, line;

		ifstream fin;
		ofstream fout;
		fin.open("/export/home/wyatt/public_html/!Data/shakespeare.txt");
		fout.open("s_jmsaline.txt");

		cout << endl;
		fout << endl;

		cout << "Please enter a word or phrase to be searched for: ";
		cin >> search;


						do
						{

						cout << search <<endl;
						getline(fin, search);

						}while(!fin.eof());

				fin.close();

				cout << endl;
				fout << endl;

				//close outputting file
				fout.close();
return 0;
}
 



i feel like i am right on the verge of having this thing done...

this is what i have to do

http://www.jbwyatt.com/163/a/a5.htm



but i cant quite figure out how to get it to read the lines where there is a certain name.

all i get is it reading out the entire file.


help.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading Specific Lines

#2 xafltexans  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 19-March 09

Re: Reading Specific Lines

Posted 02 April 2009 - 08:09 PM

anyone?
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Reading Specific Lines

Posted 02 April 2009 - 08:21 PM

You're overwriting your search term. What you want to do is to read a line, compare, if it matches, exit? if not, increment line counter, then display the data:

string temp;
int lineNum = 0;
cin >> search;

do
{

//get line
getline(fin, temp);

if(strcmp(temp.c_str(), search.c_str()) == 0)
{
	  //they match do whatever
}

//increment lineNum counter for final data report
lineNum++;
}while(!fin.eof());




edit: You'll want to increment either way seeing as you need to display the line number later.

This post has been edited by KYA: 02 April 2009 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 xafltexans  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 19-March 09

Re: Reading Specific Lines

Posted 02 April 2009 - 08:25 PM

no, except for the line number part yeah.

if u check the website that i gave.

i have to have it print out that whole line.

and then i have to have it counting them occurences,
but i cant have that much advanced coding that u have...
im not that in depth in the class and he would know i got help...

This post has been edited by xafltexans: 02 April 2009 - 08:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Reading Specific Lines

Posted 02 April 2009 - 08:32 PM

I guess I read it wrong, but the instructor is being misleading by telling you to use getline() but have you search for substrings. It will only get more complicated by searching for occurrences within a string.


Specifically:

string temp;
int lineNum = 0;
cin >> search;

do
{

//get line
getline(fin, temp);

//search temp for the search entry
//if some is found
//increment occurrence counter
// store char # beginning

//increment lineNum counter for final data report
lineNum++;
}while(!fin.eof());



As for the comparison I would use a for loop to linearly search through the string, looking for a substring. I'm sure there's an algorithm out there on DIC, do you have to write it from scratch?
Was This Post Helpful? 0
  • +
  • -

#6 xafltexans  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 19-March 09

Re: Reading Specific Lines

Posted 02 April 2009 - 08:35 PM

yeah i wrote it from scratch and what i had up top, is what i wrote so far.

this is the hardest program we've had yet.. and i hate it.. because i never had problems before now, im like bashing my head.
Was This Post Helpful? 0
  • +
  • -

#7 xafltexans  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 19-March 09

Re: Reading Specific Lines

Posted 02 April 2009 - 08:42 PM

and for that if statement i dont understand how to make it do.

if search is in line.
{
write out line
line number++
}
else
{
line number++
}


ugh...
Was This Post Helpful? 0
  • +
  • -

#8 xafltexans  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 19-March 09

Re: Reading Specific Lines

Posted 02 April 2009 - 08:48 PM

like this

						do
						{

						pos = text.find("search");

						getline(fin, search);
						}while(!fin.eof());



if position doesnt find search it's gunna be -1 then.


so how do i get it to print out ONE line.

if (pos!=-1)
{
//WHAT DO I PUT HERE!??!?!?!??!?!?!
linenum++
}
else
{
linenum++
}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1