RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 3596 | Replies: 15
Reply
Join Date: Nov 2005
Posts: 3
Reputation: slobo7x is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
slobo7x's Avatar
slobo7x slobo7x is offline Offline
Newbie Poster

Searching a file for a string

  #1  
Nov 26th, 2005
Hello all. I have a small homework problem to write, and I could use a little help, someone to guide me in the right direction.

I need to write a program that asks the user to enter the filename, and then enter the string to search in that file. If the string is found within a file, the program should display that line and the line number. Here is my code so far, I've read the chapter on this and it really didn't help at all.

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

int main()
{

fstream fin;
char fileName[21];
char line[500];
char string;

cout << "Enter the name of file you wish to open: ";
cin.getline(fileName, 21);
if(!fin)
{
cout << fileName << " could not be opened. \n";
return 0;
}
int lines = 0;
cout << "Enter your search string: ";
cin >> string;
if(string > 65 && string < 122)
{
fin.seekg(0,ios::beg);
cout << string << endl;
lines ++;
}

fin.close();
return 0;
}

Thanks for any help!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2005
Posts: 78
Reputation: perniciosus is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
perniciosus's Avatar
perniciosus perniciosus is offline Offline
Junior Poster in Training

Re: Searching a file for a string

  #2  
Nov 26th, 2005
no need to redo what has already been done
grep -n word file
or to use it in that context once you got the string do
char *arg[5] ;
arg[0] = "grep" ; arg[1] = "-n" ; arg[2] = string.c_str() ; arg[3] = fileName.c_str() ;
arg[4] = NULL ;
execvp(arg[0], arg) ;
(Your not using unix, when do one ever use console apps in windows ? go fetch mingw and tools )
Reply With Quote  
Join Date: Nov 2005
Posts: 3
Reputation: slobo7x is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
slobo7x's Avatar
slobo7x slobo7x is offline Offline
Newbie Poster

Re: Searching a file for a string

  #3  
Nov 26th, 2005
I have no idea what you just said...
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,565
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 977
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Searching a file for a string

  #4  
Nov 26th, 2005
#include <iostream>
#include <fstream>
using namespace std;

int main()
{

	fstream fin;
	char fileName[21];<< MS-Windows filenames can be 255 characters
	char line[500];
	char string; <<< this is NOT a character array, only ONE character

	cout << "Enter the name of file you wish to open: ";
	cin.getline(fileName, 21);
	if(!fin)
	{
		cout << fileName << " could not be opened. \n";
		return 0;
	}
	int lines = 0;
	cout << "Enter your search string: ";
	cin >> string;<< use getline() here so that the
search string can contain spaces
What is the below for?  useless.
	if(string > 65 && string < 122) << ?????
	{
        fin.seekg(0,ios::beg);
		cout << string << endl;
		lines ++;
	}
Well you now have the filename, so you need to
declare an object of type ifstream, open the file, read each line, search the line for the search string
and finally close the file.  Looks like you still have some work
ahead of you.	  
   
	return 0;
}
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: Searching a file for a string

  #5  
Nov 26th, 2005
If you're working with strings this might work (not tested), but it looks like dragons solution is probably what your looking for.

bool find_string(ifstream& file, const string& search)
{
  string line = " ";
  while( getline(file, line, '\n') )
  {
     if (line == search)
     {
        return true;
     }
  }

  return false;
}
Reply With Quote  
Join Date: Nov 2005
Posts: 3
Reputation: slobo7x is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
slobo7x's Avatar
slobo7x slobo7x is offline Offline
Newbie Poster

Re: Searching a file for a string

  #6  
Nov 26th, 2005
Thank you both very much, this did help me out.
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Searching a file for a string

  #7  
Nov 27th, 2005
Originally Posted by perniciosus
no need to redo what has already been done
grep -n word file
or to use it in that context once you got the string do
char *arg[5] ;
arg[0] = "grep" ; arg[1] = "-n" ; arg[2] = string.c_str() ; arg[3] = fileName.c_str() ;
arg[4] = NULL ;
execvp(arg[0], arg) ;
(Your not using unix, when do one ever use console apps in windows ? go fetch mingw and tools )

What kind of answer is this?
Reply With Quote  
Join Date: Nov 2005
Posts: 78
Reputation: perniciosus is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
perniciosus's Avatar
perniciosus perniciosus is offline Offline
Junior Poster in Training

Re: Searching a file for a string

  #8  
Nov 27th, 2005
Originally Posted by WolfPack
What kind of answer is this?

Ask a stupid question and youll get a stupid answer... btw it did solve the question: How do I find word in file with line nr...

And how do you do those fancy modifications to quotes ? (with color and stuff?, whats the command?)

Btw IMO questions that would not have resulted in such a result would have been:
1) How do I read lines from a file?
1.5) (For stupid people) How do I know what line number I am at
2) How do I find a word in said line?
3) How do I print a line number and line to indicate found string?
3.5) (For stupid people) How do I add 1 + 2 + 3 to make a program that finds words in a file ? (I'm not sure this one deserves an answer)...

This atleast shows that the person understands what he wants to do... if he does not he might want to start with
0) What is a file, word and line... and can you somehow find a word in a line that is in a file or something ? (Not sure if this would be a c/c++ question though)...
/pern.*/i

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Searching a file for a string

  #9  
Nov 27th, 2005
Originally Posted by perniciosus
Ask a stupid question and youll get a stupid answer... btw it did solve the question: How do I find word in file with line nr...
Well I thought his question was detailed enough. After all he said
Originally Posted by slobo7x
I need to write a program that asks the user to enter the filename, and then enter the string to search in that file. If the string is found within a file, the program should display that line and the line number.
and even produced some code of his own. Telling him to install mingw was a little better than telling him to remove windows and install Linux instead.


Originally Posted by perniciosus
And how do you do those fancy modifications to quotes ? (with color and stuff?, whats the command?)
The use of tags.
[code][ /CODE] tags give the colour to the enclosed code. There ought to be instructions on how to use the tags somewhere in this site.

Originally Posted by perniciosus
Btw IMO questions that would not have resulted in such a result would have been:
1) How do I read lines from a file?
1.5) (For stupid people) How do I know what line number I am at
2) How do I find a word in said line?
3) How do I print a line number and line to indicate found string?
3.5) (For stupid people) How do I add 1 + 2 + 3 to make a program that finds words in a file ? (I'm not sure this one deserves an answer)...

This atleast shows that the person understands what he wants to do... if he does not he might want to start with
0) What is a file, word and line... and can you somehow find a word in a line that is in a file or something ? (Not sure if this would be a c/c++ question though)...
Yep, nice format for asking questions. But I thought the original question was close enough.
Reply With Quote  
Join Date: Nov 2005
Posts: 78
Reputation: perniciosus is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
perniciosus's Avatar
perniciosus perniciosus is offline Offline
Junior Poster in Training

Re: Searching a file for a string

  #10  
Nov 27th, 2005
About color check, Ancient Dragons post, that was what I was talking about...
And I dont consider producing code that could just as well be copy pasted from just about any project showing that he has tried to understand or solve the problem... and if you read his post you se that he says nothing about what operating system he is using, so I could just as well assume he was using linux and was nice enough to point out what to do if he was not...

Mind solving this one:
You guys I was given a task to solve the integer factorization problem efficiently, preferable using the general number field sieve, mind helping me out (look code, I have made an effort)..
#include <iostream>
#include <string>
int main( int argc, char **argv )
{
  std::string number ;
  std::cout << "What number do you want to factorize: " ;
  std::getline( std::cin, number ) ;
  std::string factors ;
  // so I'm stuck here, I could use some help implementing the general number field   
  // sieve, perferable in a easily paralizable manner so that I can use all my 
  // availible computers to solve the problem...
  // ofcourse if you have a faster algorithm for 100+ digits numbers I would appreciate
  // an implementation of that as well....
  std::cout << number << " factors into " << factors << std::endl ;
  return 0 ;
}
/end irony
/pern.*/i

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 5:10 am.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC