- #1
ermines
- 45
- 0
umm, anyone who could help me write a code wherein, given an input file, it searches for a word inputed by the user, then outputs the result (how many times it appeared in the input file) in an output file. This program also uses 2 command line arguments. The first argument is the word to search for, the second is the name of the input file.
Below is my code which DOESN'T work. Any ideas how to correct it...?
int main (int argc, char *argv[])
{
ifstream inStream;
ofstream outStream;
if (argc != 3) //argc should be 3 for correct execution
cout << "Please enter 2 valid command-line arguments! \n";
else
{
ifstream the_ifile(argv[2]); //argv[2] is the filename to open
ofstream the_ofile("counter.txt");
if (!the_ifile.is_open()) //checking to see if file opening succeeded
cout << "Could not open file\n";
else
{
char *v, *w, *x, *y, *z;
int occurances=0;
do{
inStream >> v >> w >> x >> y >> z;
do{
occurances++;
}while(v==" " && w=="f" && x=="o" && y=="x" && z==" ");
}while(!inStream.eof());
the_ofile << "FILE: " << argv[2] << endl;
the_ofile << "SEARCHKEY: " << argv[1] << endl;
the_ofile << "OCCURANCES: " << occurances-1 << endl;
the_ofile << "---------------------------------------" << endl;
}
inStream.close();
outStream.close();
return 0;
}
}
basically, I'm trying to view from the input file 5 characters at once and check if it is the word " fox ". If it is, the variable occurances would be incremented by one. It does this until it reaches the end of the file. As more "fox" occurs, occurances would be successively incremented.
Below is my code which DOESN'T work. Any ideas how to correct it...?
int main (int argc, char *argv[])
{
ifstream inStream;
ofstream outStream;
if (argc != 3) //argc should be 3 for correct execution
cout << "Please enter 2 valid command-line arguments! \n";
else
{
ifstream the_ifile(argv[2]); //argv[2] is the filename to open
ofstream the_ofile("counter.txt");
if (!the_ifile.is_open()) //checking to see if file opening succeeded
cout << "Could not open file\n";
else
{
char *v, *w, *x, *y, *z;
int occurances=0;
do{
inStream >> v >> w >> x >> y >> z;
do{
occurances++;
}while(v==" " && w=="f" && x=="o" && y=="x" && z==" ");
}while(!inStream.eof());
the_ofile << "FILE: " << argv[2] << endl;
the_ofile << "SEARCHKEY: " << argv[1] << endl;
the_ofile << "OCCURANCES: " << occurances-1 << endl;
the_ofile << "---------------------------------------" << endl;
}
inStream.close();
outStream.close();
return 0;
}
}
basically, I'm trying to view from the input file 5 characters at once and check if it is the word " fox ". If it is, the variable occurances would be incremented by one. It does this until it reaches the end of the file. As more "fox" occurs, occurances would be successively incremented.