I read this code from C++ Primer( 5th ed,by Stanley B.Lippman) This code is used for counting the time of appearance of a letter in a file.
I wanna know the function of the 2 parameters of the main function
and, what is infile.open(argv[1],infile)
also, can the parameter of the infile.open()
not be a const? If not, how can I change the file I want to read from the console but not from the source code.
Here is the code:
//enter code here
void runQueries(ifstream &infile)
{
// infile is an ifstream that is the file we want to query
TextQuery tq(infile); // store the file and build the query map
// iterate with the user: prompt for a word to find and print results
while (true) {
cout << "enter word to look for, or q to quit: ";
string s;
// stop if we hit end-of-file on the input or if a 'q' is entered
if (!(cin >> s) || s == "q") break;
// run the query and print the results
print(cout, tq.query(s)) << endl;
}
}
// program takes single argument specifying the file to query
//enter code here
int main(int argc, char **argv)
{
// open the file from which user will query words
ifstream infile;
// open returns void, so we use the comma operator XREF(commaOp)
// to check the state of infile after the open
if (argc < 2 || !(infile.open(argv[1]), infile)) {
cerr << "No input file!" << endl;
return EXIT_FAILURE;
}
runQueries(infile);
return 0;
}