You may have encountered this problem before–your program contains a full path name of a file, into which data has to be written or read from:
FILE * f = fopen("C:dataemployees.txt", "r"); //ERROR!
However, at run time, you encounter unexpected behavior when the program fails to read from, or write to, the specified file, seemingly for no reason.
Well, there is a reason. A character preceded by a ” is considered as a special character in C/C++. For example, ‘
‘ designates a new line, ‘ ‘ designates a tab, etc. Even non-reserved sequences such as ‘d’ and ‘e’ (both appearing in the full path name above) are treated each as a single character. In order to interpret a slash in a string correctly, it has to be preceded by another slash. Therefore, the correct path string should be:
FILE * f = fopen("C:\data\employees.txt", "r"); //correct: '\' is interpreted as '