C/Visual C - how to know if files exist or not??

 
Vista:

how to know if files exist or not??

Publicado por KarSec (2 intervenciones) el 02/05/2002 20:55:01
in C++ or C?
how to know when a file already exist in the disk??
that way I don't overwrite it with a new file that the program is creating??

thanks
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:how to know if files exist or not??

Publicado por Ismael (230 intervenciones) el 03/05/2002 00:12:45
Read this example:

#include "stdio.h"
void main(int argc,char *argv[])
{
FILE *fp;
char c;
if(argc!=2) //argv[1] is the name of the file you want to open
{
printf("\nERROR, Parametros incorrectos");
exit(1);
}
if(fp = fopen(argv[1],"r") == NULL) //reading mode
printf("No se puede abrir el archivo");
else
{
printf("\nEl archivo existe. Y sera cerrado")
fclose(fp)
}
//your code...
} // End of main

suerte

Ismael: www.geocities.com/ismaelcamarero

Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:how to know if files exist or not??

Publicado por Alexis Leung (70 intervenciones) el 03/05/2002 19:30:19
Very easy. Here is an example:

FILE *my_file; /* the pointer */

my_file = fopen("C:\another_file.dat","rb");

You have to open the file in read mode, then you have to check if the pointer is NULL or not, if is NULL then the file doesn't exist.

if (my_file == NULL) /* if doesn't exsit */
printf("ERROR: CANNOT OPEN THE FILE, MAKE SURE THE PATH OR FILENAME IS CORRECT");
else /* if exist */
{
.....
.....
}

Remember it must be "rb".
If you use "w" or "wb" then you will overwrite the file or create a new file in case that the file doesn't exist.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar