Lecture d´un fichier texte .
Je ne comprend pas pourquoi ce tout simple bout de code ne fonctionne .
erreur : la mémoire ne peut pas être read ...
(le fichier existe )
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <stdio.h>
FILE *fmap ;
int lettre;
charger_fichier_map(char *chemin_fichier){
fmap = fopen(chemin_fichier,"r");
while ((lettre = fgetc(fmap)) != EOF)
{
printf("%c",lettre); /* O/P the character to the screen */
}
fclose(fmap);
}
int main(int argc, char *argv[])
{
charger_fichier_map("C:\dl.txt");
system("PAUSE");
return 0;
} |
question auxilliaire :
si j´ai plusieurs ligne dans mon .txt , comment faire pour savoir quand je suis au bout d´une ligne ? est ce possible en utilisant juste fgetc ?
merci d´avance !
Re: Lecture d´un fichier texte .
Citation:
Envoyé par pilouface
Je ne comprend pas pourquoi ce tout simple bout de code ne fonctionne .
On pourraut avoir le code qui compile ? On ne sait pas ce que tu as oublé...
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
Compiling: main.c
main.c:1: error: syntax error before '*' token
main.c:1: warning: type defaults to `int' in declaration of `fmap'
main.c:1: warning: data definition has no type or storage class
main.c:5: warning: return type defaults to `int'
main.c:5: warning: no previous prototype for 'charger_fichier_map'
main.c: In function `charger_fichier_map':
main.c:6: error: implicit declaration of function `fopen'
main.c:6: warning: nested extern declaration of `fopen'
main.c:6: warning: assignment makes pointer from integer without a cast
main.c:8: error: implicit declaration of function `fgetc'
main.c:8: warning: nested extern declaration of `fgetc'
main.c:8: error: `EOF' undeclared (first use in this function)
main.c:8: error: (Each undeclared identifier is reported only once
main.c:8: error: for each function it appears in.)
main.c:10: error: implicit declaration of function `printf'
main.c:10: warning: nested extern declaration of `printf'
<internal>:0: warning: redundant redeclaration of 'printf'
main.c:13: error: implicit declaration of function `fclose'
main.c:13: warning: nested extern declaration of `fclose'
main.c: At top level:
main.c:18: error: conflicting types for 'main_'
C:/clib/frmwrk/inc/main.h:36: error: previous declaration of 'main_' was here
main.c:18: error: conflicting types for 'main_'
C:/clib/frmwrk/inc/main.h:36: error: previous declaration of 'main_' was here
main.c:19:21: warning: unknown escape sequence '\d'
main.c: In function `main_':
main.c:19: warning: passing arg 1 of `charger_fichier_map' discards qualifiers from pointer target type
main.c:21: error: implicit declaration of function `system'
main.c:21: warning: nested extern declaration of `system'
main.c:23:2: warning: no newline at end of file
Process terminated with status 1 (0 minutes, 0 seconds) |
Citation:
question auxilliaire :
si j´ai plusieurs ligne dans mon .txt , comment faire pour savoir quand je suis au bout d´une ligne ? est ce possible en utilisant juste fgetc ?
La fin de ligne est marquée par '\n'.
Mais pour lire des lignes de texte, fgets() est tout indiqué.
http://emmanuel-delahaye.developpez....tes.htm#saisie
http://emmanuel-delahaye.developpez....s.htm#fichiers