I am writing a program that counts the number of words in a text file.
How would I go about useing a command line argument as the text file name that will be opened? Right now i am opening a file in the same directory as the program.(test.txt file)
for example: wordcount somefile.txt
would open "somefile" and count the words in it.
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
int main (int argc, char * argv[] )
{
int wordcount = 0;
int letters = 0;
double average = 0;
char ch;
ifstream infile;
infile.open("test.txt");
if (infile.fail( ))
{
cout << "Input file opening failed.\n";
system("pause");
exit(1);
}
}