Error in opening a file

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 key to understanding this is the errors you get.

1
2
3
'C' : unrecognized character escape sequence
'D' : unrecognized character escape sequence
'g' : unrecognized character escape sequence


I have seen this several times myself, so your not alone.

Look at your errors, look at the first char after each \ below
fileName = "C:\\Users\Cristian\Desktop\gettys.txt"; // obtain the full file name

The fix should be put in another \ when using a path.

fileName = "C:\\Users\\Cristian\\Desktop\\gettys.txt"; // obtain the full file name


'C' : unrecognized character escape sequence
'D' : unrecognized character escape sequence
'g' : unrecognized character escape sequence
fileName = "C:\\Users\Cristian\Desktop\gettys.txt";

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";

See "Character and string literals" on this page: http://www.cplusplus.com/doc/tutorial/constants/
Last edited on
Yup totally figured it out then went back to this posting to say I got it...but you had already answered....Thanks a lot
after trying to run your code, I noticed you should change void main () to int main().

You should also post code blocks around your code so we can refer to line numbers.

-Thx
Last edited on
Topic archived. No new replies allowed.