PDA

View Full Version : restituire codice diverso da 0


trunks81
11-03-2005, 18:45
Ciao a tutti!
Il programmino che sto facendo deve leggere all'avvio un file di nome prova.txt nella home directory dell'utente che esegue il comando, però può anche succedere che il comando non venga lanciato dalla home directory).
Quindi mi si chiede che se il file .txt non esiste, il comando deve termnare restituendo un codice di ritorno diverso da 0;

ecco cosa ho scritto:

/*trovo l'ambiente dovè prova.txt**/
char *ambiente;
ambiente=getenv("HOME");
if(ambiente==NULL) return -1;
char path[80];
sprintf(path,"%s/prova.txt",ambiente);

/* Apro il fileprova.txt*/
IFERROR(fd=open(path, O_RDONLY), "aprendo prova.txt");

il problema è che se non trova il file nella
HOME, mi si blocca senza che mi dica nulla.

Come faccio a farmi dare un messaggio d'errore?
Grazie!

ciao!

trunks81
12-03-2005, 09:35
UP

AIUTO PLEASE
:confused: :confused:

Fenomeno85
12-03-2005, 11:00
if ((fp=fopen (...))==NULL){
printf ("\nErrore non ce sta il file");
return (-1);
}

~§~ Sempre E Solo Lei ~§~

trunks81
12-03-2005, 12:13
Originariamente inviato da Fenomeno85

if ((fp=fopen (...))==NULL){
printf ("\nErrore non ce sta il file");
return (-1);
}

~§~ Sempre E Solo Lei ~§~
Ho messo questo al posto di
IFERROR.....

ma mi da segmentation fault

a cosa è dovuto?

Fenomeno85
12-03-2005, 12:52
if ((fp=fopen ("file.txt", "r"))==NULL){
printf ("\nErrore non ce sta il file");
return (-1);
}


strano hai messo così???

~§~ Sempre E Solo Lei ~§~

trunks81
12-03-2005, 14:08
Ho scritto così:


/*trovo l'ambiente dovè prova.txt**/
char *ambiente;
ambiente=getenv("HOME");
if(ambiente==NULL) return -1;
char path[80];
sprintf(path,"%s/prova.txt",ambiente);

if ((fd=fopen ("file.txt", "r"))==NULL){
printf ("\nErrore non ce sta il file");
return (-1);
}


mi da sempre SEGMENTATIO FAULT
non è che è un problema di dimensione path[80]?

grazie....

Fenomeno85
12-03-2005, 15:38
per i percorsi dai le costanti si sistama:

#include <stdlib.h>

These constants define the maximum length for the path and for the individual fields within the path.

Constant Meaning
_MAX_DIR Maximum length of directory component
_MAX_DRIVE Maximum length of drive component
_MAX_EXT Maximum length of extension component
_MAX_FNAME Maximum length of filename component
_MAX_PATH Maximum length of full path

~§~ Sempre E Solo Lei ~§~

trunks81
12-03-2005, 16:56
stdlib.h l'ho incluso!!!

dov'è l'errore??