| | |
Searching a file for a string
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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!
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!
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
(Your not using unix, when do one ever use console apps in windows ? go fetch mingw and tools )
grep -n word file
or to use it in that context once you got the string do
C++ Syntax (Toggle Plain Text)
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) ;
#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;
}•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
If you're working with strings this might work (not tested), but it looks like dragons solution is probably what your looking for.
C++ Syntax (Toggle Plain Text)
bool find_string(ifstream& file, const string& search) { string line = " "; while( getline(file, line, '\n') ) { if (line == search) { return true; } } return false; }
•
•
•
•
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
(Your not using unix, when do one ever use console apps in windows ? go fetch mingw and tools )C++ Syntax (Toggle Plain Text)
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) ;
•
•
•
•
Originally Posted by WolfPack
What kind of answer is this?
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
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
•
•
•
•
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...
•
•
•
•
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.
•
•
•
•
Originally Posted by perniciosus
And how do you do those fancy modifications to quotes ? (with color and stuff?, whats the command?)
[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)...
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)..
/end irony
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)..
C++ Syntax (Toggle Plain Text)
#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 ; }
/pern.*/i
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Please make my frist socket app work...
- Next Thread: Linked List & Objects
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






