Solved

File Existe ??

Posted on 1998-10-31
10
220 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

757 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

18 Experts available now in Live!

Get 1:1 Help Now