#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0

    How to load file in linked liste


    hello everyone can any one help me to do this functionn ...thanks to you alll :)


    this is my structure
    struct data
    {
    int ref;
    char nom[20];
    char prenom[20];
    char tel[11];
    struct data *next; // pointeur vers une structure data
    };

    // On crée une structure PERSON de type data
    typedef struct data PERSON;
    // On cree LINK de type PERSON *
    typedef PERSON *LINK;

    this is my functin
    LINK load(LINK head)
    {
    // Declaration des variables
    char *ext=".txt";
    int rf;
    char nom[21];
    char nom_f[21];
    char prenom_f[21];
    char tel_f[11];
    FILE *entree;
    LINK current;
    current=head;
    printf("Chargement : \n");
    // Saisie du nom de fichier
    printf("Entrez le nom du fichier (sans extension) :");
    scanf("%s",nom);
    strcat(nom,ext);
    if ((entree=fopen(nom,"r"))==NULL) // Si l'ouverture s'est mal passée
    {
    printf("Erreur a l'ouverture du fichier %s. \n",nom);// Message d'erreur
    system("PAUSE");
    exit(0);
    }
    else // Sinon
    {
    fscanf(entree,"%d\n",current->ref);
    fscanf(entree,"%s\n",nom_f);
    fscanf(entree,"%s\n",prenom_f);
    fscanf(entree,"%s\n",tel_f);
    while (!feof(entree))
    {
    ajout(head,rf,nom_f,prenom_f,tel_f);
    fscanf(entree,"%d\n",current->ref);
    fscanf(entree,"%s\n",nom_f);
    fscanf(entree,"%s\n",prenom_f);
    fscanf(entree,"%s\n",tel_f);
    ; // fin while
    printf("Chargement Reussi !!");
    system("PAUSE");
    // Ferme le fichier
    fclose(entree);
    };//fi else
    }; // fin if
    };//fin fnction

    void ajout(LINK head,int rf,char nom_f[21],char prenom_f[21],char tel_f[11])
    {
    printf("\nAjouter un nouvel element en tete de liste");
    LINK current;
    current=malloc(sizeof(PERSON)); // creation d'une nouvelle structure cel et stockage de son adresse dans surf
    current->ref=rf;
    strcpy(current->prenom,prenom_f);
    strcpy(current->nom,nom_f);
    strcpy(current->tel,tel_f);
    current->next=head;
    head=current;
    };
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    First thing: use code tags! You need to use code tags to keep HTML from removing the indentation of your code, thus removing your formatting, thus making your code an unreadable mess.

    Second thing: format your code!!!! By neglecting to format your code, even you cannot read it! Remember: you aren't formatting for us, but rather you are formatting for yourself. If even you cannot read your own code, then how could you ever hope to debug it?

    Here is what your code looks like when properly formatted. I will highlight a highly questionable portion in red:
    Code:
    LINK load(LINK head)
    {
        // Declaration des variables
        char *ext=".txt";
        int rf;
        char nom[21];
        char nom_f[21];
        char prenom_f[21];
        char tel_f[11];
        FILE *entree;
        LINK current;
        current=head;
        printf("Chargement : \n");
        // Saisie du nom de fichier
        printf("Entrez le nom du fichier (sans extension) :");
        scanf("%s",nom);
        strcat(nom,ext);
        if ((entree=fopen(nom,"r"))==NULL) // Si l'ouverture s'est mal passée
        {
            printf("Erreur a l'ouverture du fichier %s. \n",nom);// Message d'erreur
            system("PAUSE");
            exit(0);
        }
        else // Sinon
        {
            fscanf(entree,"%d\n",current->ref);
            fscanf(entree,"%s\n",nom_f);
            fscanf(entree,"%s\n",prenom_f);
            fscanf(entree,"%s\n",tel_f);
            while (!feof(entree))
            {
                ajout(head,rf,nom_f,prenom_f,tel_f);
                fscanf(entree,"%d\n",current->ref);
                fscanf(entree,"%s\n",nom_f);
                fscanf(entree,"%s\n",prenom_f);
                fscanf(entree,"%s\n",tel_f);
                ; // fin while
                printf("Chargement Reussi !!");
                system("PAUSE");
                // Ferme le fichier
                fclose(entree);
            };//fi else
        }; // fin if
    };//fin fnction
    Why are you closing the file after having only read the first record? Also, why didn't you bother to inform us of the problem you are having? Of the symptoms that made you think that something was wrong? Would you go to the doctor feeling ill but refuse to describe your symptoms to him? No, that would make absolutely no sense at all. So why do that to us?

    Look at this line:
    ; // fin while
    Do you think that maybe you should have placed the while loop's close brace there instead of after you close the file? Do you think that maybe that is what you intended to do? Maybe you mistook that extraneous semicolon for a close brace. For that matter, why all the extra useless semicolons after the braces? All you're accomplishing is to confuse yourself. If you had at least formatted your code you would have immediately seen the structure of your code, but instead you chose to conceal valuable information from yourself. Are you starting to understand why it is so important that you format your code?

    And what's with the extra close brace? : }; // fin if
    The compiler complained and you threw it in without thinking? Don't you know where the if-statement's close brace is? It's right before the else // Sinon

    By the way, that is a very good idea you're using. Commenting the close braces so that you readily know what it matches up with. You just need to make sure that they truly match up with what you think they do. In reality, as revealed by formatting the code, the "fi else" actually matches with the while and the extraneous "fin if" should be the true "fi else". Add in the "fin while" where you intended it and get rid of the extraneous "fin if" and that should correct a lot.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0
    after correcting the loop i had a problem in this function it's shows 'conflicting types for"ajout"
    Code:
    
    void ajout(LINK head,int rf,char nom_f[21],char prenom_f[21],char tel_f[11]) 
    
    { 
    printf("\nAjouter un nouvel element en tete de liste");
     LINK current;
     current=malloc(sizeof(PERSON)); // creation d'une nouvelle structure cel et stockage de son adresse dans surf
    
     current->ref=rf;
     strcpy(current->prenom,prenom_f); strcpy(current->nom,nom_f);
     strcpy(current->tel,tel_f); 
    current->next=head; 
    head=current; 
    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0
    my problem is that i couldn't load my file.txt in the linked liste if you will give me the function that load my information saved in the file in a linked liste that will be a great favor fromm youu
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by oussamaboun
    after correcting the loop i had a problem in this function it's shows 'conflicting types for"ajout"
    The code fragment you posted shows ajout being defined after your call to that function. Did you declare a prototype for ajout before the function call? The compiler works its way from the top of the file to the bottom and it must know about an identifier (eg, typedef, variable, function) before you try to use it.

    Legacy default behavior in C is that it will let you use an identifier before you declare it, but the default is that a variable is int and that a function returns int and has one parameter which is an int. If you do not prototype a function or place it before the call, the compiler will make those assumptions about it and then when you finally get around to actually declaring it the compiler will complain that this new declaration conflicts with the default "declaration" it already has.

    Ensure that you prototyped your function, ajout, and that the prototype matches the function definition. Also, in more than 20 years I've never put the array sizes in the parameter list and so I don't know what kind of problems that could cause.

    PS
    Pay attention to the wording of the warning and error messages you get and remember what the actual problems were that had generated those messages. With experience, you will learn to know immediately what the most probable cause of a new error is. That is why the words "conflicting types" immediately said to me that you had probably left out the function prototype. Remember, we've all made the same mistakes before, which is why we know what to look for. Though some beginners here somehow manage to come up with some very novel mistakes.
    Last edited by dwise1_aol; January 11th, 2013 at 10:48 AM.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by oussamaboun
    my problem is that i couldn't load my file.txt in the linked liste if you will give me the function that load my information saved in the file in a linked liste that will be a great favor fromm youu
    We will help you work it out, but we cannot do your work for you.

    First you need to clean up the problems that you know you already have so that you can get a clean compile. Make sure that you set the compiler to display warnings and then do not ignore the warnings. Warnings are much more important than error messages are and should never be ignored.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0
    thanks to your advices finaly i coud fix all the problms my program is working noww thanks to you alll

IMN logo majestic logo threadwatch logo seochat tools logo