•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 361,914 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,580 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 3191 | Replies: 15
![]() |
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
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) ;
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 9,886
Reputation:
Rep Power: 32
Solved Threads: 793
#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
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
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.
bool find_string(ifstream& file, const string& search)
{
string line = " ";
while( getline(file, line, '\n') )
{
if (line == search)
{
return true;
}
}
return false;
}•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,480
Reputation:
Rep Power: 8
Solved Threads: 98
•
•
•
•
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 )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) ;
What kind of answer is this?
•
•
•
•
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
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,480
Reputation:
Rep Power: 8
Solved Threads: 98
•
•
•
•
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)..
#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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Please make my frist socket app work...
- Next Thread: Linked List & Objects



Linear Mode