show struct....

This is a discussion on show struct.... within the C Programming forums, part of the General Programming Boards category; Hi, My little progs work except it just show the first value of my struct 6 times instead of showing ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    14

    show struct....

    Hi,

    My little progs work except it just show the first value of my struct 6 times instead of showing my 6 differents value....

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    FILE * ptrPerson;
    FILE * ptrOutput;


    struct Employe {
    char prenom[20];
    char nom[20];
    }Names[6];
    struct Employe Remplir(struct Employe Temp);
    void afficher(struct Employe Temp);

    int cnt;

    main()
    {
    struct Employe Names[6];
    for(cnt=0;cnt<=6;cnt++){
    Names[cnt]=Remplir(Names[cnt]);
    }
    for (cnt=0;cnt<=6;cnt++){
    afficher(Names[cnt]);
    }

    return 0;
    }



    struct Employe Remplir(struct Employe Temp){
    if ((ptrPerson=fopen("person.dat","r+b"))==NULL)
    {
    printf("\nErreur: le fichier n'a pu Ítre ouvert.\n");
    exit(1);
    }
    {
    fseek(ptrPerson,0L,SEEK_SET);
    fscanf(ptrPerson,"%s%s\n",Temp.nom,Temp.prenom);
    fclose(ptrPerson);
    return(Temp);
    }

    }

    void afficher(struct Employe Temp){
    printf("%s %s\n",Temp.nom,Temp.prenom);
    }

    thnaks

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    14
    shall I use fread instead of fscanf???

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    You need to pass the structure to the function by address. Otherwise, changes made to the structure will not be "remembered" when it leaves the function.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You need to pass the structure to the function by address
    Not when you return the structure that was read into and assign it immediately.

    The problem is in how you open the file. Every time you call Remplir, it opens the file and sets the position to 0, then reads from the file and closes it. For this reason you read the same data every time, whatever is at the beginning of the file. A better way is to open the file in main and simply read from it in Remplir.
    Code:
    #include <stdio.h> 
    #include <conio.h> 
    #include <stdlib.h>
     
    FILE * ptrPerson; 
    FILE * ptrOutput; 
    
    struct Employe { 
      char prenom[20]; 
      char nom[20]; 
    }Names[6]; 
    
    struct Employe Remplir(struct Employe Temp); 
    void afficher(struct Employe Temp); 
    
    int cnt; 
    
    main() 
    { 
      struct Employe Names[6];
      if ((ptrPerson=fopen("datain.txt","r+b"))==NULL) 
      { 
        printf("\nErreur: le fichier n'a pu Ítre ouvert.\n"); 
        exit(1); 
      }
      else
      { 
        for(cnt=0;cnt<6;cnt++){ 
          Names[cnt]=Remplir(Names[cnt]); 
        } 
        for (cnt=0;cnt<6;cnt++){ 
          afficher(Names[cnt]); 
        }
        fclose(ptrPerson);
      }
      return 0; 
    } 
    
    struct Employe Remplir(struct Employe Temp){ 
      fscanf(ptrPerson,"%s%s\n",Temp.nom,Temp.prenom);  
      return(Temp); 
    } 
    
    void afficher(struct Employe Temp){ 
      printf("%s %s\n",Temp.nom,Temp.prenom); 
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    14

    Thumbs up

    Thanks a lot prelude!!!! very very appreciated!!!!

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    14
    Hey,

    I now have to write those data in a new files, but in oppossite order..... the first name at the end etc....

    I thought making
    for(cnt=7;cnt=0;cnt--){
    righttofile(Names[cnt]);
    }

    will work, but it simply write nothing....I wonder why this don't work...and what the good method!

    thx

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I'd need to see more code.

    >for(cnt=7;cnt=0;cnt--){
    Two problems. First, your array is only six elements long, meaning that the last valid subscript is 5, not 7. Second, your condition doesn't test cnt for 0, it assigns 0 to cnt. Both of these will surely not work like you want.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  4. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21