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!