Solved

File Existe ??

Posted on 1998-10-31
10
247 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stack 22 172
Console based application in Linux 1 93
List out all word 7 318
Loading flat file data in tables 2 41
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

829 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