• C

File Existe ??

Hi,

I would like to know to to look in the current directory
if a file is already exist ? Also how to list files in the
directory.

Thank !
woainiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
what operating system?
woainiAuthor Commented:
Hi Ozo,

I really don't know how to answer to this question. I program in Visual C++ 5, but
I program in C. The environment is Win NT.


woainiAuthor Commented:
Hi Ozo,

I really don't know how to answer to this question. I program in Visual C++ 5, but
I program in C. The environment is Win NT.


IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

woainiAuthor Commented:
Okie, Can you tell me if it exist in C language a function or way to look at the current
directory and list all files in it ? and put all the contents in a string.

Thank a lot.
ozoCommented:
directory reading is not specified in the C language
but readdir() is part of the POSIX standard.
Answers2000Commented:
1. To test if file exists using _stat runtime library function

struct _stat buffer ;
int ret = _stat( "name.xxx", &buffer ) ;

The buffer return contains info on the file

ret is 0 if it worked
ret is -1 if it failed (usually file doesn't exist) - you can double check reason for failure by checking value of errno global variable.

This works in VC5 but is somewhat portable to other compilers.


2. To find files in a directory use Win32 API functions

FindFirstFirst,
repeatedly call FindFirstNext
when done call FindClose

This is Win32 only.  Will work on most compilers include VC5.


All  of the above are shown in VCs help.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
woainiAuthor Commented:
Okie, Can you tell me if it exist in C language a function or way to look at the current
directory and list all files in it ? and put all the contents in a string.

Thank a lot.
Answers2000Commented:
There isn't a standard portable function for this in C.  It is operating system / platform dependant

You mention you are using Visual C++ on NT, so you use Windows specific function

Here's a sample (excuse any typos, as I just typed it straight in).  Compile as Win32 console app, non-Unicode.

/* Make sure you link to Windows libraries - e.g. in a Win32 console app */
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main( int argc, char * argv[] )
{
char szSomeDir[] = "c:\\" ;
HANDLE hh ;
WIN32_FIND_DATA data ;
char szSearchPath[256] ;
BOOL bOK = TRUE ;

strcpy( szSearchPath, szSomeDir ) ;
strcat( szSearchPath, "*.*" ) ;
hh = FindFirstFile(  szSearchPath, &data ) ;
if ( hh == INVALID_HANDLE_VALUE )
{
printf( "Can't open directory\n" ) ;
exit(1) ;
}
while ( bOK )
{
   // examine "data" here for information (n.b. will include sub-directories as well,
  // the data struct includes elements for this
  printf( "%s\n", (LPCSTR)(data.cFilename) ) ;

  // get next file
  bOK = FindNextFile( hh, data ) ;
}
FindClose(hh) ;

return 0 ;
}



Answers2000Commented:
Oops...

bOK = FindNextFile( hh, data ) ;

should be

bOK = FindNextFile( hh, &data ) ;

woainiAuthor Commented:
Thanks a lot Answers2000.

I will try your example. It is nice to you to give me an example. Because My administrator
don't install the help for my Visual so I can't do anything.

Thank
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.