Netmaster
Netmaster

Reputation: 287

Working with pointers in C

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

I had a problem with the file, but now, my problem is with my structure struct departement *recherche_dpt(char recherche_code[]), it always returns CODE_NON_TROUVE = 0.

How should I solve this, please?

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<stddef.h>
#include <errno.h>
#define MAX_dpt 100000
#define KEY_NOT_FOND "NOT_FOUND"

/* global statement */
struct departement
{
    char currentCode[10];
    char oldCode[10];
    char name[50];
    struct departement *predecesseur ;
    struct departement *successeur;
};
struct departement *debut_liste, *fin_liste;
struct departement *undepartement();
void ouvrir_fichier(char Nomfichier[]);
struct departement *search_dpt(char recherche_code[]);
struct departement tabdpt[MAX_dpt];

int main()
{
    char sexe, reponse, date[5], annee[3], mois[4], bidon[3],birthPlace[30], ordre[4], struct1[6], struct2[6],nir1[12],nir[13],nir2[13], nirancien[13] ;
    int i, namebre, cle, reste,n;
    long int val;

    struct departement undpt, *pointeur;
    char code[10];

    scanf("%s",birthPlace);

    // convert birthplace uppercase and assign it to name
    int k = 0;
    while(birthPlace[k])
    {
        birthPlace[k] = toupper(birthPlace[k]);
        k++;
    }
    printf("%s\n", birthPlace);

    // Read in the file
    ouvrir_fichier("Basedecommunes.txt");
    pointeur=search_dpt(birthPlace);
    undpt = *pointeur;

    if (strcmp(undpt.currentCode,KEY_NOT_FOND)==0)
    {
        printf("No ""%s"" has been found \n",birthPlace);
    }
    else
    {
        printf("Current code   : %s\n",undpt.currentCode);
        printf("Old code   : %s\n",undpt.oldCode);
        printf("Department name    : %d\n",undpt.name);
    }


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

        nb=0;
        f1=fopen(Nomfichier, "r" );
        if (f1 == NULL) {
            printf("fopen failed, errno = %d\n", errno);
        }
        else {
            printf("fopen succeeded\n");

            while ((! feof(f1)) && (nb < MAX_dpt) )
            {
                ptmp=undepartement();
                if (ptmp != NULL) {

                    lire=fscanf (f1, "%s %s %s", (*ptmp).currentCode, (*ptmp).oldCode, (*ptmp).name);
                    printf("lire = %d\n",lire);
                    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++ ;
                    }
                } else {
                    printf("malloc error\n");
                }
            }

        }

        fclose(f1);
    }


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

        trouve = 0 ;

        pointeur=undepartement();

        strcpy((*pointeur).currentCode, KEY_NOT_FOND);

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

            trouve=((strcmp(ptmp.currentCode,dep_name))==0) ;

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

        return pointeur;

    }

    /* --- allocation memoire d'une nouvelle structure --- */
    struct departement * undepartement(void)
    {
        struct departement *p = malloc(sizeof *p);
        return p;
    }

The file content:

01001   01001   ABERGEMENT-CLEMENCIAT
01002   01002   ABERGEMENT-DE-VAREY
01003   01003   AMAREINS
01004   01004   AMBERIEU-EN-BUGEY
01005   01005   AMBERIEUX-EN-DOMBES
01006   01006   AMBLEON
01007   01007   AMBRONAY
01008   01008   AMBUTRIX
01009   01009   ANDERT-ET-CONDON
01010   01010   ANGLEFORT
01011   01011   APREMONT
01012   01012   ARANC
01013   01013   ARANDAS
01014   01014   ARBENT
01015   01015   ARBIGNIEU
01016   01016   ARBIGNY

Upvotes: 0

Views: 128

Answers (2)

Atul
Atul

Reputation: 4340

Inside while loop you are comparing .currentCode with .dep_name. This was wrong. Change the code to this:

// trouve=((strcmp(ptmp.currentCode,dep_name))==0) ;
trouve=((strcmp(ptmp.name,dep_name))==0) ;

And it works correct.

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

In your code,

 strcpy((*pointeur).codeactuel, CODE_NON_TROUVE);

codeactuel is defined to have a size of 10, but to copy CODE_NON_TROUVE you need to have a size of 11.

Then, printf(birthPlace); is also wrong, it should be something like

 printf("%s\n", birthPlace);

Also, there is a logical problem in

pointeur=undepartement();

if malloc() fails and undepartement() returns NULL, you'll be facing undefined behaviour by dereferencing pointeur. Please add a NULL check.

P.S - There maybe other issues. Please provide a MCVE and if possible kindly stick to english so that we can understand the code logic in a better way.

Upvotes: 2

Related Questions