How would I search a txt file for a specific line?

Text pointer position, read/write from/to file

Page 1 of 1

7 Replies - 2185 Views - Last Post: 27 October 2010 - 05:27 AM Rate Topic: -----

#1 Cloudyeyes  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 15-May 10

How would I search a txt file for a specific line?

Posted 26 October 2010 - 01:39 PM

I'm designing a program where it searches a txt file for a specific line of text and then displays it, via a certain reference or ID.

For example, I have a text file...

123aaa Dog
abc321 Cat
zyx987 Mouse

Now, the first column are the IDs or references. I want to have the user input a specific ID, in this case, abc321. The program will then copy the entire second line and display it onto the screen. How can I position the pointer to the second line from this reference ID?

I understand I could just copy the whole text file into memory, but I don't want to do that, as the text file could hold millions of records. I simply want to create a class to temporarily hold the contents of whatever line the user just searched for.

Any help is appreciated! :)

This post has been edited by Cloudyeyes: 26 October 2010 - 01:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How would I search a txt file for a specific line?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: How would I search a txt file for a specific line?

Posted 26 October 2010 - 01:49 PM

Read a line
Use a library function to search for the desired text
If found print line

Can't get much simpler than that.
Was This Post Helpful? 1
  • +
  • -

#3 Cloudyeyes  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 15-May 10

Re: How would I search a txt file for a specific line?

Posted 26 October 2010 - 02:08 PM

View PostJackOfAllTrades, on 26 October 2010 - 12:49 PM, said:

Read a line
Use a library function to search for the desired text
If found print line

Can't get much simpler than that.


There's a library function to search for the desired text in the file? What is it?
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How would I search a txt file for a specific line?

Posted 27 October 2010 - 04:04 AM

View PostCloudyeyes, on 27 October 2010 - 06:08 AM, said:

There's a library function to search for the desired text in the file?

No but there are library functions to look for text in the string which holds the line you got from from the text file using getline().
http://www.cplusplus...string/getline/



View PostCloudyeyes, on 27 October 2010 - 06:08 AM, said:

What is it?

Use your best judgement to choose from the options.
http://www.cplusplus.../string/string/
Was This Post Helpful? 1
  • +
  • -

#5 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: How would I search a txt file for a specific line?

Posted 27 October 2010 - 04:29 AM

As long as the user input is identical in case, you only need to read in the desired string and compare it with the user input. Now, the complex part comes from modeling your program around the file.
If all data in the text file is formatted as your example above, you can use the fact that strings treat whitespace as a terminator and read into two strings from each line. Compare the first string against the user input, and if there is a match store the second string value. You can read each line this way.
If the text file contains multiple formats, your problem has become slightly more complex. You would need to find a way to deal with seperate formats and extract the appropriate data.
If word case is an issue, such as the user entering all lower case and the ID contains upper case, you will need to extract the data and format to lower case before you can do any verification.

EDIT: Grammar

This post has been edited by alias120: 27 October 2010 - 04:30 AM

Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How would I search a txt file for a specific line?

Posted 27 October 2010 - 04:33 AM

When you said this:

View Postalias120, on 27 October 2010 - 08:29 PM, said:

you can use the fact that strings treat whitespace as a terminator and read into two string from each line.
I think you meant
you can use the fact that iostreams treat whitespace as a terminator and read into two string from each line.
Am I right?

If it was me I would still prefer to use getline() to get a line at a time from the file and if I didn't want to parse the string in the way I imagine JackOfAllTrades was thinking I would use a stringstream to break it at whitespaces the way I think you are suggesting.
But that's just me. I like using getline(), maybe too much.

This post has been edited by janotte: 27 October 2010 - 04:38 AM

Was This Post Helpful? 1
  • +
  • -

#7 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: How would I search a txt file for a specific line?

Posted 27 October 2010 - 04:47 AM

Thank you for the correction janotte, I didn't know that was determined closer to the base. I use getline() quite often when working with obtaining data from files, but I have not used the stringstream method you described. I will look into that, especially if it simplifies the process.
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How would I search a txt file for a specific line?

Posted 27 October 2010 - 05:27 AM

@alias120
Thought a little play program might be helpful for showing how stringstreams could be used as we were discussing. Not sure it is all that elegant but wanted to confirm to myself that it would work as I thought I remembered. Happily it does. Would have been embarrassing otherwise.
#include <iostream>
#include <sstream>
#include <string>

int main(){
	std::string playStr         = "the quick brown fox";
	std::string playStrArray[4] = {"*","*","*","*"};
	std::stringstream playStrStm;
	
	playStrStm << playStr;
	
	std::cout << "----------" << std::endl;
	for(int i=0; i<4; i++){
		std::cout << "element[" << i << "] = " 
		          << playStrArray[i] << std::endl;
	}
	std::cout << "----------" << std::endl;
	
	
	for(int i=0; i<4; i++){
		playStrStm >> playStrArray[i];
	}
    
	std::cout << "----------" << std::endl;
	for(int i=0; i<4; i++){
		std::cout << "element[" << i << "] = " 
		          << playStrArray[i] << std::endl;
	}
	std::cout << "----------" << std::endl;
    getchar();
}


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1