Solved

File Existe ??

Posted on 1998-10-31
10
229 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
 

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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
c language help - file paths 7 140
voltage to force translation ? 8 97
Best UNIX-compatible free C compiler for Windows or Mac 6 237
An API detour question 7 79
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now