Lire une ligne à partir d'un fichier en c

khalil -  
lami20j Messages postés 21331 Date d'inscription   Statut Modérateur, Contributeur sécurité Dernière intervention   -
Bonjour,
j'ai cette procedure avec laquelle je veux afficher une ligne du fichier qui contient un mot saisit par l'utilisateur
void recherche()
#define TAILLE_MAX 1000
{
char chaine[TAILLE_MAX]="";
char nom[20];
FILE *fp;
printf("donner le nom du pays : ");
scanf("%s",&nom);
fp=fopen("article.txt","r");
if(fp==nom)
{while (fgets(chaine,TAILLE_MAX,fp)==nom)
printf("%s",chaine);}
getch();
fclose(fp);


}
quand je compile il me dit nonportable pointer comparaison
c pour le if (fp==nom)
merci
A voir également:

4 réponses

amigo
 
bonjour,

fp=fopen("article.txt","r");

la fonction fopen retourne un pointeur de valeur non NULL si elle à abouti et NULL si elle a echoué.

donc il faut ecrire
if (fp == NULL)
{
printf ("\nERREUR D'OUVERTURE DU FICHIER : article.txt\n");
exit (1);
}

A+.
0
lami20j Messages postés 21331 Date d'inscription   Statut Modérateur, Contributeur sécurité Dernière intervention   3 570
 
Salut,

pour compléter
fopen retourne un pointeur de type FILE sur un fichier

fp==nom
juste pour te dire qu'une comparaison entre 2 chaines (ici tu croyas que fp contiendra le nom de fichier)
se fait en utilisant la fonction strcmp et pas == (erreur classique)
0
gasy
 
comment fais tu pour l'utilisation de strcmp
0
lami20j Messages postés 21331 Date d'inscription   Statut Modérateur, Contributeur sécurité Dernière intervention   3 570
 
Salut,

par exemple
lami20j@debian:~/trash$ cat strcmp.c
#include<stdio.h>
#include<string.h>

int main ()
{
  char s1[20], s2[20];

  printf ("Chaine1 ? ");
  scanf("%s",s1);
  printf ("CHaine2 ? ");
  scanf("%s",s2);

  switch (strcmp (s1, s2)) {
  case -1:
    printf ("%s < %s\n",s1,s2);
    break;
  case 0:
    printf ("%s == %s\n",s1,s2);
    break;
  case 1:
    printf ("%s > %s\n",s1,s2);
    break;
  }
  return 0;
}
lami20j@debian:~/trash$ gcc strcmp.c
lami20j@debian:~/trash$ ./a.out
Chaine1 ? gasy
CHaine2 ? lami20j
gasy < lami20j
lami20j@debian:~/trash$ ./a.out
Chaine1 ? gasy
CHaine2 ? gasy
gasy == gasy
lami20j@debian:~/trash$ ./a.out
Chaine1 ? gasy
CHaine2 ? ccm
gasy > ccm
0