How do I change the initial part of the program where it doesn't prompt a user to input the path of the text file they want to read, but automatically opens a text file once the code is ran.
I want it to specifically open s:\\windows\gettys.txt
I know I have to use input.open("s:\\windows\gettys.txt"), but when I initially did it, it didn't work.
Please help.
#include <fstream> // for ifstream
#include <iostream> // for cin, cout and cerr
#include <string> // for the string datatype
#include <cstdlib> // needed for the exit function
using namespace std; //needed to use the string datatype, notice that
// the include files do not have the .h
// extension and <iostream> is required even if
// <fstream> is used
//
// function prototypes
//
string getInputFileName(); // a function to prompt for the complete file name
int numWordsInFile( ifstream &in ); // a function to count the
// number of words and
// lines in a text file
void main ()
{
int nWords; // number of words in the text file
ifstream inFile; // handle for the input text file
string fileName; // complete file name including the path
fileName = getInputFileName(); // prompt and obtain the full file name
inFile.open(fileName.c_str()); // try to open the file
if( !inFile.is_open() ) // test for unsuccessfull file opening
{
cerr << "Cannot open file: " << fileName << endl << endl;
exit (0);
}
nWords = numWordsInFile( inFile ); // determine the number of words in the file
//
// print the number of words
//
cout << "The number of words in the file: " << fileName
<< " is = " << nWords << endl << endl;
inFile.close(); // close the input file
}
//************************************************************
//
// Function name: getInputFileName
//
// Purpose: to prompt for the fully qualified name of a file
// i.e. including the path of the file
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: a string containing the fully qualified name
// of a file
//
//************************************************************
string getInputFileName()
{
string fName; // fully qualified name of the file
cout << "Please enter the fully qualified name of the " << endl
<< "input text file (i.e. including the path): ";
cin >> fName; // cannot handle blanks in a file name or path
cout << endl; // skip a line
return fName;
}
//******************************************************************
//
// Function name: numWordsInFile
//
// Purpose: counts the number of words in a text file
// i.e. including the path of the file
//
// Input parameters: in - a file handle pointing (pass by reference)
// to the input file
//
// Output parameters: none
//
// Return Value: the number of words in the text file
//
//*******************************************************************
int numWordsInFile( ifstream &in )
{
int numWords = 0; //number of words initialized to zero
string str; // word holder;
while ( in >> str ) // get the next word from the file
// the function get will also get whitespace
// i.e. blanks, tabs and end of line words
{
if ( str != "\n" ) // test for end of line word
numWords++; // increase the count of words by one
}
return numWords; // return the number of words in the file
}