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!

Recommended Answers

All 15 Replies

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 )

I have no idea what you just said...

#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;
}

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;
}

Thank you both very much, this did help me out.

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?

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)...

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

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.

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

The use of tags.
CODE tags give the colour to the enclosed code. There ought to be instructions on how to use the tags somewhere in this site.

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.

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

About color check, Ancient Dragons post, that was what I was talking about...

very similar to code tags but use color tags insted
[ color = <color here> ] and [ /color ]
(remove the spaces from the above)

Example in red

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 )

If you think about 5 minutes before you knew this trick this post wouldn't be very helpful.

If you think about 5 minutes before you knew this trick this post wouldn't be very helpful.

What I'm saying is that you shouldn't try to make someone feel bad for not knowing what you already learned. There once was a time where you were the same way.

Hey, I was very thankful when I got to know about exec, now that is a convenient system call...

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...

Well...determining this, is subject to the individual's intelligence so I need not comment.

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...

What a valid assumption. Now it is all beginning to make sense. :rolleyes:

]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 ;
}

Saw that the question does not show genuine effort :cheesy: . Also I dont know the Alogorithm too. Better to say you dont know rather than make whatever reply you can think of.
/end retort

Well the point was that neither do I, and I am not likely to understand a solution you provide me with even if it was richly documented... From the code given it looked like he had gotten so far as to get a file name and a string to search for, and this would be a solution to the problem stated... He probably learned as much from my answer as from copy pasting what ever someone else neatly wrote down and perhaps error checked that fit directly into his code...

And of course lastly, all hail the BOFH...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.