Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: All
Sep 3, 1998

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:\data\employees.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, '\n' designates a new line, '\t' 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 '\'
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date