For some reason I am having trouble opening the file designated. I keep getting the error:
'C' : unrecognized character escape sequence
'D' : unrecognized character escape sequence
'g' : unrecognized character escape sequence
I don't really know that this means...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
//
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 = "C:\\Users\Cristian\Desktop\gettys.txt"; // 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: 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
}
The '\' character is used for special characters, such as '\n' for newline and so on.
Your filename contains '\C', '\D' and '\g'.
The answer (as you started out doing correctly), is to use '\\' every time you want a real '\' character. fileName = "C:\\Users\\Cristian\\Desktop\\gettys.txt";