Solved

File Existe ??

Posted on 1998-10-31
10
239 Views
Last Modified: 2010-04-15
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 !
0
Comment
Question by:woaini
  • 5
  • 3
  • 2
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1253977
what operating system?
0
 

Author Comment

by:woaini
ID: 1253978
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.


0
 

Author Comment

by:woaini
ID: 1253979
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.


0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:woaini
ID: 1253980
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253981
directory reading is not specified in the C language
but readdir() is part of the POSIX standard.
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 30 total points
ID: 1253982
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.
0
 

Author Comment

by:woaini
ID: 1253983
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.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1253984
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 ;
}



0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1253985
Oops...

bOK = FindNextFile( hh, data ) ;

should be

bOK = FindNextFile( hh, &data ) ;

0
 

Author Comment

by:woaini
ID: 1253986
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
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dll Dynamic way c/c++ 9 145
Convert image to byte array 8 203
undefined reference to `bswap_128' 9 165
How to translate this 2-line while loop into C from Perl? 8 96
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question