Netmaster
Netmaster

Reputation: 287

C file open error

I have created this program in c. The user type 'lieunaissance' and then we open a formatted file to search 'lieunaissance' value.

The problem is that when the program try to open the file, I get an error message.

How should I access the file?

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<stddef.h>
#define MAX_dpt 100000
#define CODE_NON_TROUVE "NON_TROUVE"

struct departement
{
char codeactuel[10];
char anciencode[10];
char nom[50];
struct departement *predecesseur ;
struct departement *successeur;
};
struct departement *debut_liste, *fin_liste;
struct departement *undepartement();
void ouvrir_fichier(char Nomfichier[]);
struct departement *recherche_dpt(char recherche_code[]);
struct departement tabdpt[MAX_dpt];
void ouvrir_fichier();

int main()
{
    char sexe, reponse, date[5], annee[3], mois[4], bidon[3],lieunaissance[30], ordre[4], struct1[6], struct2[6],nir1[12],nir[13],nir2[13], nirancien[13] ;
    int i, nombre, cle, reste,n;
    long int val;
    struct departement undpt, *pointeur;
    char code[10];

    scanf("%s",lieunaissance);

    // convertir lieu de naissance en majuscule et l'affecter à nom
    int k = 0;
    while(lieunaissance[k])
    {
        lieunaissance[k] = toupper(lieunaissance[k]);
        k++;
    }
    printf(lieunaissance);

    //Lire dans le fichier
    ouvrir_fichier("Basedecommunes.txt");
    pointeur = recherche_dpt(lieunaissance);
    undpt    = *pointeur;
    if (strcmp(undpt.codeactuel,CODE_NON_TROUVE)==0)
    {
        printf("Aucun ""%s"" n'a ete trouve \n",lieunaissance);
    }
    else
    {
        printf("Code actuel   : %s\n",undpt.codeactuel);
        printf("Code ancien   : %s\n",undpt.anciencode);
        printf("Nom du departement    : %d\n",undpt.nom);
    }
}

void ouvrir_fichier(char Nomfichier[])
{
    struct departement *ptmp, *prec, *succ;
    FILE *f1;
    int nb, lire;

    //printf("Entrez le nom du fichier :");
    //scanf("%s",Nomfichier);

    nb = 0;
    f1 = fopen(Nomfichier, "r");
    if (f1 == NULL)
    {
        printf("Probleme acces fichier\n");
    }
    else
    {
        while ((! feof(f1)) && (nb < MAX_dpt) )
        {
            ptmp = undepartement();
            lire = fscanf (f1, "%s %s %s", (*ptmp).codeactuel, (*ptmp).anciencode, (*ptmp).nom);
            if (lire != EOF)
            {
                (*ptmp).predecesseur = NULL ;
                (*ptmp).successeur   = NULL;
                if (debut_liste == NULL)
                {
                    debut_liste = ptmp;
                    fin_liste   = ptmp;
                }
                else
                {
                    (*fin_liste).successeur = ptmp;
                    (*ptmp).predecesseur    = fin_liste;
                    fin_liste               = ptmp;
                }
                nb++ ;
            }
        }
        /* if (! (nb < MAX_dpt) )
        {
        printf("Nb departement = %d \n",MAX_dpt);
        }
        printf("Nb dpt lu = %d\n",i); */
    }
    fclose(f1);
}


/*--- fonction de recherche --- */
struct departement *recherche_dpt(char recherche_code[])
{
    struct departement ptmp, *pointeur, *pactu;
    int trouve ;

    trouve   = 0;
    pointeur = undepartement();
    strcpy((*pointeur).codeactuel, CODE_NON_TROUVE);

    pactu = debut_liste;
    while ((!trouve) && (pactu != NULL))
    {
        ptmp  = (*pactu);
        pactu = (*pactu).successeur;

        trouve = ((strcmp(ptmp.codeactuel,recherche_code)) == 0) ;

        if (trouve)
        {
            *pointeur = ptmp ;
        }
    }

    return pointeur;
}

/* --- allocation m?moire d'une nouvelle structure --- */
struct departement *undepartement()
{
    return (struct departement *) undepartement(sizeof(struct departement));
}

Upvotes: 0

Views: 1088

Answers (2)

n0p
n0p

Reputation: 3496

From manpage:

RETURN VALUE Upon successful completion fopen(), fdopen() and freopen() return a FILE pointer. Otherwise, NULL is returned and errno is set to indicate the error.

So you might want to #include <errno.h> and check errno when your fopen fails to have your answer.

My guess: maybe your file Basedecommunes.txt is not in the same directory of your running program.

Upvotes: 1

unwind
unwind

Reputation: 399703

This:

struct departement *undepartement()
{
    return (struct departement *) undepartement(sizeof(struct departement));
}

is very wrong, it tries to return the address of the function (!) converted to a pointer to a structure, but also does an infinite recursive call that will eat your stack and cause undefined behavior.

It should just be:

struct departement * undepartement(void)
{
    struct departement *p = malloc(sizeof *p);
    return p;
}

Note that functions accepting no arguments in C should be declared as (void). Also note that malloc() can fail, you need to check the return vaule of undepartement() before relying on it.

Upvotes: 3

Related Questions