www.digitalmars.com         C & C++   DMDScript  

D - checking if a file exists

reply "Carlos" <carlos8294 msn.com> writes:
Since I don't remember well file functions in C (which are used in D), I
have wrote this to check if a file exists:

char[] file="temp";
FILE *F;
int fileLength=0;
F=fopen(file,"w+");
for (;! feof(F);fileLength++)
    fgetc(F);
fclose(F);
if (fileLength==0) ...

But this if is always false. It doesn't matter what I use instead of "temp",
but the if is always false. Why? It should perfectly work (as far as I
understand).

Another way that I tried was opening the file for read only, but I couldn't
catch the exception (or error). This was the idea: if an error ocurred, then
the file doesn't exist. But I can't make anything to make the program
continue after the error. It's easily done in Basic (sorry if I talk about
Basic too much), because you can do:

ON ERROR GOTO label
...
ON ERROR GOTO 0
...
label:
flag=1
RESUME NEXT

If you put the ... GOTO label line before opening the file, then it goes to
label, sets a flag and continues running. After that, the error trapper is
deactivated. Is there any way to do something like that in D?

-------------------------
Carlos 8294
http://carlos3.netfirms.com/
May 28 2002
parent reply "Carlos" <carlos8294 msn.com> writes:
Why is it so weird? If I add a fgetc(F) before the for, it works. What is
it? It has to read the file to know it's empty?

"Carlos" <carlos8294 msn.com> escribió en el mensaje
news:ad1dj3$1gsc$1 digitaldaemon.com...
 Since I don't remember well file functions in C (which are used in D), I
 have wrote this to check if a file exists:

 char[] file="temp";
 FILE *F;
 int fileLength=0;
 F=fopen(file,"w+");
 for (;! feof(F);fileLength++)
     fgetc(F);
 fclose(F);
 if (fileLength==0) ...

 But this if is always false. It doesn't matter what I use instead of
"temp",
 but the if is always false. Why? It should perfectly work (as far as I
 understand).

 Another way that I tried was opening the file for read only, but I
couldn't
 catch the exception (or error). This was the idea: if an error ocurred,
then
 the file doesn't exist. But I can't make anything to make the program
 continue after the error. It's easily done in Basic (sorry if I talk about
 Basic too much), because you can do:

 ON ERROR GOTO label
 ...
 ON ERROR GOTO 0
 ...
 label:
 flag=1
 RESUME NEXT

 If you put the ... GOTO label line before opening the file, then it goes
to
 label, sets a flag and continues running. After that, the error trapper is
 deactivated. Is there any way to do something like that in D?

 -------------------------
 Carlos 8294
 http://carlos3.netfirms.com/
May 28 2002
parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Carlos wrote:

 Why is it so weird? If I add a fgetc(F) before the for, it works. What is
 it? It has to read the file to know it's empty?
Yes, this is an old problem with the C stdlib. The EOF flag on the file descriptor is not set until you read the EOF from the file. Remember, fgetc returns an int...which can be the character in the file, or the EOF value. I've long hated this design, but you learn to restructure your algorithms around it if you use it a lot. On UNIX machines, you can use stat() to get information about a file (including its length) - I forget what the Windows equivalent is. If you decide to do the fgetc() route, I would suggest you open the file in "r" mode if it's a text file or "rb" mode if it's a binary file. In "w+" mode, you create an (empty) file if it does not exist. -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ]
May 29 2002
parent reply "Carlos" <carlos8294 msn.com> writes:
 If you decide to do the fgetc() route, I would suggest you open the file
in "r"
 mode if it's a text file or "rb" mode if it's a binary file.  In "w+"
mode, you
 create an (empty) file if it does not exist.
That's the other way I was telling about. The problem is that opening a non-existing file in read-only mode, there's always an error. It doesn't matter what language or platform is used, it's always an error. So, the alternative is to catch that error and force the program to keep running, but I can't find a way to do it in D.
May 29 2002
parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Carlos wrote:

 If you decide to do the fgetc() route, I would suggest you open the file
in "r"
 mode if it's a text file or "rb" mode if it's a binary file.  In "w+"
mode, you
 create an (empty) file if it does not exist.
That's the other way I was telling about. The problem is that opening a non-existing file in read-only mode, there's always an error. It doesn't matter what language or platform is used, it's always an error. So, the alternative is to catch that error and force the program to keep running, but I can't find a way to do it in D.
Why not test the FILE* against NULL? Isn't that what fopen() returns on an error? -- The Villagers are Online! http://villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ]
May 29 2002