how to access a file in C:\

  • Thread starter Francesco Zavatarelli
  • Start date
F

Francesco Zavatarelli

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:\\Program Files\\Palm\\Tungst\\Backup\\SmsDB.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\Tungst\Backup\SmsDB.PDB"; I get a
compiler error: lexical: unknown escape sequence `\P'
 
D

Darksun4

Francesco Zavatarelli said:
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:\\Program Files\\Palm\\Tungst\\Backup\\SmsDB.PDB";

try char *NAME_SMS="c:\Program Files\Palm\Tungst\Backup\SmsDB.PDB";

file_sms = fopen(NAME_SMS, "r");
if (file_sms == NULL) {
fprintf(stdout, "Error opening file.\n");
}

fclose(file_sms);

Be sure that file_sms is a FILE * .
When I run the code I get the "Error opening file." message. If I try with
char* NAME_SMS = "c:\Program Files\Palm\Tungst\Backup\SmsDB.PDB"; I get a
compiler error: lexical: unknown escape sequence `\P'

If that don't work, what is the name of the compiler you are using?
 
E

Emmanuel Delahaye

Francesco Zavatarelli said:
char* NAME_SMS = "c:\\Program Files\\Palm\\Tungst\\Backup\\SmsDB.PDB";

Because of the presence of ' ' (SPACE) in the path name, your system probably
requires to wrap the string by double quotes. Also, it probably knows that
'/' is an alternative to '\\'

char* NAME_SMS = "\"c:/Program Files/Palm/Tungst/Backup/SmsDB.PDB\"";
 
E

Emmanuel Delahaye

Darksun4 said:
try char *NAME_SMS="c:\Program Files\Palm\Tungst\Backup\SmsDB.PDB";

No. It's fully wrong.
If that don't work, what is the name of the compiler you are using?

Any decent C compiler will refuse to compile such a code.
 
C

Chiron Paixos

]
char* NAME_SMS = "c:\\Program Files\\Palm\\Tungst\\Backup\\SmsDB.PDB";

[...]

Blanks in pathnames sometimes cause strange effects.
You might try encapsulating the pathname in " " like:

char* NAME_SMS = "\"c:\\Program
Files\\Palm\\Tungst\\Backup\\SmsDB.PDB\"";

(It's been quite a while since my last C programming activities, so
it's just an educated guess)
 
M

Martin Ambuhl

Darksun4 said:
If that don't work, what is the name of the compiler you are using?

If it *does* work, what is the name of the compiler *you* are using?
What meaning does it give to the escape sequences '\P', '\T', '\B', and
'\S'?
 
D

Darrell Grainger

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:\\Program Files\\Palm\\Tungst\\Backup\\SmsDB.PDB";

As a few people have mentioned, the space in the path could be causing the
problem. It could also be a limitation of the compiler. Older MSDOS
compilers expect the name to be 8.3. When you have a path like "Program
Files" the compiler actually wants "PROGRA~1". This is a quirk of
MSDOS/Windows. You might want to check a newsgroup that is more familar
with your compiler (you didn't mention which one) or with your operating
system (Windows or MSDOS).
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\Tungst\Backup\SmsDB.PDB"; I get a
compiler error: lexical: unknown escape sequence `\P'

The \P is definitely wrong. If you need a '\' in the path you need to put
'\\'.
 
B

Bernhard Holzmayer

Francesco said:
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:\\Program
Files\\Palm\\Tungst\\Backup\\SmsDB.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\Tungst\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
 
C

Christopher Benson-Manica

Francesco Zavatarelli said:
char* NAME_SMS = "c:\\Program Files\\Palm\\Tungst\\Backup\\SmsDB.PDB";

const char *NAME_SMS;

String literals are not modifiable in the absence of certain compiler
extensions, so your declaration should reflect that.
 
E

Emmanuel Delahaye

const char *NAME_SMS;

String literals are not modifiable in the absence of certain compiler
extensions, so your declaration should reflect that.

Good point.
 
A

Arthur J. O'Dwyer

Francesco said:
char* NAME_SMS = "c:\\Program Files\\Palm\\Tungst\\Backup\\SmsDB.PDB";
[...]
The reason why it fails, is obviously bc. of how escape sequences
are handled.

Aha! Someone who uses "obviously" even more inappropriately than
myself! ;-) s/bc. of/not because of/
\\ evaluates to \ in the first stage of the lexical parsing,

Terminology is important in this newsgroup. \\ isn't an expression
and doesn't "evaluate" to anything. Nor does the first phase of
translation do anything special with backslashes. You might say
that "Doubled backslashes inside strings 'turn into' single backslashes
in the compiled program," with more elaboration depending on context
and relevancy.
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.

Wrong, of course. You may be confusing C strings with Perl
quoting operators, which do all kinds of weird stuff. There's nothing
magic about the percent sign in C, and the only magic thing about
backslashes is the way doubled ones turn into single ones, only
once, during translation phase 5.
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.

Then it's misguided, because taken literally it's very, very wrong.
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.

This I find hard to believe. Do you still have the code anywhere,
or can you reconstruct it accurately from memory? I *have* had
occasion to use an octuple-backslash (line 61 of
http://www.contrib.andrew.cmu.edu/~ajo/free-software/quine.c
) but that was in a toy program done purely for hack value, not
for any practical purpose.

Another possibility:
Some compilers understand forward slashes and will interpret them
correctly, so you'd try
char* NAME_SMS = "c:/Program.../SmsDB.PDB";

This is not a compiler issue (all C compilers understand forward
slashes, since otherwise how could you divide two numbers?) but
rather an operating-system issue (most OSes understand slashes at
the system level, with some obvious exceptions and some not-so-
obvious non-exceptions, such as MS-DOS and Win32).
Then, to indicate the drive letter, try this one (used @ cygnus gcc)
char* NAME_SMS = "//c/Program...

This is a Unix convention and is highly unlikely to work on the
OP's Windows OS.
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/...

One backslash ==> compiler diagnostic.
Two backslashes ==> might work, but I doubt it. (Untested.)

-Arthur
 
D

David Resnick

Francesco Zavatarelli said:
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:\\Program Files\\Palm\\Tungst\\Backup\\SmsDB.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\Tungst\Backup\SmsDB.PDB"; I get a
compiler error: lexical: unknown escape sequence `\P'

You probably want to be asking in a windows specific group, as
your issue is quite platform specific.

[OT stab at answering, your mileage may vary]
There are two things you might try:
1) Substitute Progra~1 for "Program files". Not perfect, but it will usually
work unless there is another directory at the root of the C drive starting
with progra..., in which case it is not a good solution. If you to use
the backslash file name scheme you may need to resort to this.

2) Best solution: use forward slashes in your path. It'll just work then.
The following works just fine for me...

#include <stdio.h>

int main()
{
const char* NAME_SMS = "c:/Program Files/testing";

FILE *file_sms = fopen(NAME_SMS, "r");
if (file_sms == NULL) {
fprintf(stdout, "Error opening file '%s'.\n", NAME_SMS);
}
else {
fclose(file_sms);
}

return 0;
}


[OT]

-David
 
K

Kenneth Brody

Francesco Zavatarelli wrote:
[...]
char* NAME_SMS = "c:\\Program Files\\Palm\\Tungst\\Backup\\SmsDB.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\Tungst\Backup\SmsDB.PDB"; I get a
compiler error: lexical: unknown escape sequence `\P'

With all of these replies concentrating on your non-existent "problem"
with the backslashes, no one has pointed out the obvious:

Use perror() to determine why it failed.

Perhaps you have a typo in the name, and the filename you are using doesn't
exist? Perhaps the file is open in "exclusive mode" by another program, and
you cannot open it because of this? Perhaps you don't have permissions to
access this file?
 
K

Kenneth Brody

Emmanuel said:
Because of the presence of ' ' (SPACE) in the path name, your system probably
requires to wrap the string by double quotes. Also, it probably knows that
'/' is an alternative to '\\'

char* NAME_SMS = "\"c:/Program Files/Palm/Tungst/Backup/SmsDB.PDB\"";

Not unless the filename actually contains those quotes. There is no
problem with spaces in the filename, as you are not passing it to a
program which may be interpreting the spaces as a command-line seperator.
 
K

Kenneth Brody

Bernhard Holzmayer wrote:
[...]
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.

Absolutely wrong! That will result in "c:\\Program ..." being passed
as the filename, and you absolutely do not want to be passing double
backslashes, as this will confuse some versions of Windows to no end.
(It will look for a system called "Program Files", just as if you were
to try the Windows-ish "\\server\sharename" format.)

This is not printf! This is fopen!

Since this is (apparently) Windows, you want to pass the filename:

c:\Program Files\Palm\...

To get the single-backslash in the string, you use double-backslashes
in the source:

"c:\\Program Files\\Palm\\..."

Which is exactly what he has.

On a side note, DOS and Windows have always taken forwardslashes in
path names, so you could just as easily use:

"c:/Program Files/Palm/..."

However, this will have no effect on whatever the real problem is here.

[...]
 
J

Joe Wright

Kenneth said:
Not unless the filename actually contains those quotes. There is no
problem with spaces in the filename, as you are not passing it to a
program which may be interpreting the spaces as a command-line seperator.

You are probably mistaken. Most command line parsers treat 'space'
as a separator. Micro$oft does. The explicit quotes combine the
enclosed characters into one argument.
 
R

Richard Bos

Joe Wright said:
You are probably mistaken. Most command line parsers treat 'space'
as a separator. Micro$oft does.

Erm... yes. But I think Kenneth's point was that command line parsers
are immaterial, since this string does _not_ get passed to a command
line parser, but to fopen(). fopen() _should_ expect a single file name,
not a command line.

Richard
 
B

Bernhard Holzmayer

Sorry, I got something wrong:
I thought compiler wound complain \\P with the last lexical error
about escape sequence \P

Therefore I assumed that the parser might be broken or handling
stuff strangely.

With correct reading of the original post, I agree to your
correction - and apologize for my inadequate post.

Bernhard
 
B

Bernhard Holzmayer

Francesco said:
char* NAME_SMS = "c:\\Program Files\\Palm\\Tungst\\Backup\\SmsDB.PDB";

file_sms = fopen(NAME_SMS, "r");
if (file_sms == NULL) {
fprintf(stdout, "Error opening file.\n");
}

fclose(file_sms);

I checked with gnu (cygnus) on Win2000 Prof.,

writing path name like
const char* NAME_SMS =
"//C/Program Files/Palm/Tungst/Backup/SmsDB.PDB";

works. Give it a try.

Bernhard
 
F

Francesco Zavatarelli

Back after a few days...

1)the c compiler I'm using is "Miracle C" see http://www.c-compiler.com
anybody knows it? Anybody can give me an advice of a low cost c compiler?



2)and I am having a lot of troubles with the code. It seems it is not
related to the ' ' inside the string.

In fact I'm using the following code now (with the italian version of the
path which has Programmi instead of Program Files). Must be a BIG errore tat
I cannot see at all...

---start of code:
#include <stdio.h>
FILE* file_sms;

char* NAME_SMS = "C:\Programmi\Palm\Tungst\Backup\SmsDB.PDB";

main()
{
file_sms = fopen(NAME_SMS, "r");
if (file_sms == NULL) { fprintf(stdout, "Error opening sms file
%s\n",NAME_SMS); }
fclose(file_sms);
....etc
---endof code:

if I use char* NAME_SMS = "C:\Programmi\Palm\Tungst\Backup\SmsDB.PDB";
I get the error:
main.c: line 21: lexical: unknown escape sequence `\P'

'char* NAME_SMS = "C:\Programmi\Palm\Tungst\Backup\SmsDB.PDB"'

aborting compile



If I use char* NAME_SMS = "C:/Programmi/Palm/Tungst/Backup/SmsDB.PDB";
compiling ok but I ger an error when I run the program: Error opening sms
file C:/Programmi/Palm/Tungst/Backup/SmsDB.PDB



If I use char* NAME_SMS = "\"c:/Programmi/Palm/Tungst/Backup/SmsDB.PDB\"";
compiling is ok but still I get the error: Error opening sms file
"c:/Programmi/Palm/Tungst/Backup/SmsDB.PDB"


thanks for any suggestion.
FZ


Chiron Paixos said:
]
char* NAME_SMS = "c:\\Program Files\\Palm\\Tungst\\Backup\\SmsDB.PDB";

[...]

Blanks in pathnames sometimes cause strange effects.
You might try encapsulating the pathname in " " like:

char* NAME_SMS = "\"c:\\Program
Files\\Palm\\Tungst\\Backup\\SmsDB.PDB\"";

(It's been quite a while since my last C programming activities, so
it's just an educated guess)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top