Correct Path Names

Correct Path Names

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 '
See also  5 Tips to Manage a Remote Workforce

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist