problem listing files in a directory

J

Jrdman

hi.
i wrote that code to list the files existed in "c:\" but it doesn't
seem to work cuz when i excute it it dosn't list all the exitsed files
in "c:\"
can someone tell me what's wrong ?
thanks.
#include<stdio.h>
#include<dir.h>

#define FILE_NOT_FOUND (-1)
int main(int argc,char *argv[])
{

_finddata_t finddata;

long hfile;
hfile=_findfirst("c:\*.*",&finddata);
while(_findnext(hfile,&finddata)!= FILE_NOT_FOUND){
printf("%s\n",finddata.name);
}
_findclose(hfile);
getchar();
return 0;
}
 
U

user923005

hi.
i wrote that code to list the files existed in "c:\" but it doesn't
seem to work cuz when i excute it it dosn't list all the exitsed files
in "c:\"
can someone tell me what's wrong  ?
thanks.
#include<stdio.h>
#include<dir.h>

#define FILE_NOT_FOUND  (-1)
int main(int argc,char *argv[])
{

    _finddata_t finddata;

    long  hfile;
    hfile=_findfirst("c:\*.*",&finddata);
    while(_findnext(hfile,&finddata)!= FILE_NOT_FOUND){
    printf("%s\n",finddata.name);}

_findclose(hfile);
getchar();
    return 0;



}


It's a FAQ:

19.17: Why can't I open a file by its explicit path? The call

fopen("c:\newdir\file.dat", "r")

is failing.

A: The file you actually requested -- with the characters \n and
\f
in its name -- probably doesn't exist, and isn't what you
thought you were trying to open.

In character constants and string literals, the backslash \ is
an escape character, giving special meaning to the character
following it. In order for literal backslashes in a pathname
to
be passed through to fopen() (or any other function)
correctly,
they have to be doubled, so that the first backslash in each
pair quotes the second one:

fopen("c:\\newdir\\file.dat", "r")

Alternatively, under MS-DOS, it turns out that forward slashes
are also accepted as directory separators, so you could use

fopen("c:/newdir/file.dat", "r")

(Note, by the way, that header file names mentioned in
preprocessor #include directives are *not* string literals, so
you may not have to worry about backslashes there.)
 
B

Barry Schwarz

hi.
i wrote that code to list the files existed in "c:\" but it doesn't
seem to work cuz when i excute it it dosn't list all the exitsed files
in "c:\"
can someone tell me what's wrong ?
thanks.
#include<stdio.h>
#include<dir.h>

#define FILE_NOT_FOUND (-1)
int main(int argc,char *argv[])
{

_finddata_t finddata;

long hfile;
hfile=_findfirst("c:\*.*",&finddata);

If you didn't see a diagnostic here you need to up your warning level
or find a competent compiler. \* is not a valid sequence.
while(_findnext(hfile,&finddata)!= FILE_NOT_FOUND){
printf("%s\n",finddata.name);
}
_findclose(hfile);
getchar();
return 0;
}


Remove del for email
 
R

rahul

hi.
i wrote that code to list the files existed in "c:\" but it doesn't
seem to work cuz when i excute it it dosn't list all the exitsed files
in "c:\"
can someone tell me what's wrong ?
thanks.
#include<stdio.h>
#include<dir.h>

#define FILE_NOT_FOUND (-1)
int main(int argc,char *argv[])
{

_finddata_t finddata;

long hfile;
hfile=_findfirst("c:\*.*",&finddata);
while(_findnext(hfile,&finddata)!= FILE_NOT_FOUND){
printf("%s\n",finddata.name);}

_findclose(hfile);
getchar();
return 0;

}

Strictly off-topic, as you are using a particular API which is not
covered by C standards. By the way, either use an escaped '\' or use a
'/'. Windows accepts both '\' and '/' as path separator.
 

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

Forum statistics

Threads
474,001
Messages
2,570,249
Members
46,846
Latest member
BettinaOsw

Latest Threads

Top