Francesco Zavatarelli wrote:
I downloaded a free c-compiler and I'm trying to access a file in
a windows directory c:\program files\... but I get an error when I
run the code. Probably my declaration char* NAME is wrong. Anybody
can give a suggestion? Thanks very much.
Fzavat
char* NAME_SMS = "c:\\Progra m
Files\\Palm\\Tu ngst\\Backup\\S msDB.PDB";
file_sms = fopen(NAME_SMS, "r");
if (file_sms == NULL) {
fprintf(stdout, "Error opening file.\n");
}
fclose(file_sms );
When I run the code I get the "Error opening file." message. If I
try with char* NAME_SMS = "c:\Program
Files\Palm\Tung st\Backup\SmsDB .PDB"; I get a compiler error:
lexical: unknown escape sequence `\P'
The reason why it fails, is obviously bc. of how escape sequences
are handled.
\\ evaluates to \ in the first stage of the lexical parsing,
then the sequence is searched for things like %s in a printf ...
This time it assumes that the backslash indicates that the following
token is meant as some special character / escape sequence.
If the resulting string should really contain backslashes as folder
separators, it might help to do an additional masking like
char* NAME_SMS = "c:\\\\Program. ..
That's not a joke.
In a certain chain of cascaded parsing stages in a real project,
I had to insert 8 backslashes in every place where you'd expect one
in the final output. So, don't wonder - take it easy - if it works.
Another possibility:
Some compilers understand forward slashes and will interpret them
correctly, so you'd try
char* NAME_SMS = "c:/Program.../SmsDB.PDB";
Then, to indicate the drive letter, try this one (used @ cygnus gcc)
char* NAME_SMS = "//c/Program...
// indicates the top level or root directory (like in UNIX) here.
The blank will certainly add additional problems as the other
posters indicated.
One chance is to merely escape it by one or two backslashes,
char* NAME_SMS = "//c/Program\ Files/... or
char* NAME_SMS = "//c/Program\\ Files/...
Another possibility is to add it using its character code (0x20).
Last not least, you could check if the compiler provides special
functions for this. Because it obviously doesn't feel comfortable
with the Windows-like path names, it will probably provide
translation methods for conversion between Windows-like path names
and maybe unix- or mac-like.
If you would like to check this, scan the include directory for
sub-directories with names like win, win32, sys, dos, local.
If nothing helps, reveal which compiler (and version) it is. Maybe
some of us know it.
Bernhard