|
||||
|
Section 15:
|
[15.16] Why can't I open a file in a different directory such as "..\test.dat"?
Because "\t" is a tab character. You should use forward slashes in your filenames, even on operating systems that use backslashes (DOS, Windows, OS/2, etc.). For example:
#include <iostream>
#include <fstream>
int main()
{
#if 1
std::ifstream file("../test.dat"); // RIGHT!
#else
std::ifstream file("..\test.dat"); // WRONG!
#endif
...
}
Remember, the backslash ("\") is used in string literals to create
special characters: "\n" is a newline, "\b" is a backspace,
and "\t" is a tab, "\a" is an "alert", "\v" is a
vertical-tab, etc. Therefore the file name
"\version\next\alpha\beta\test.dat" is interpreted as a bunch of very
funny characters. To be safe, use "/version/next/alpha/beta/test.dat"
instead, even on systems that use a "\" as the directory separator.
This is because the library routines on these operating systems handle
"/" and "\" interchangeably.
Of course you could use "\\version\\next\\alpha\\beta\\test.dat", but that might hurt you (there's a non-zero chance you'll forget one of the "\"s, a rather subtle bug since most people don't notice it) and it can't help you (there's no benefit for using "\\" over "/"). Besides "/" is more portable since it works on all flavors of Unix, Plan 9, Inferno, all Windows, OS/2, etc., but "\\" works only on a subset of that list. So "\\" costs you something and gains you nothing: use "/" instead. |
|||