Like Tree1Likes
  • 1 Post By stahta01

Linked-List fails to load data for 8th node, but loads the other 10. Any clue why?

This is a discussion on Linked-List fails to load data for 8th node, but loads the other 10. Any clue why? within the C Programming forums, part of the General Programming Boards category; I am making a program in C, that creates 2 files for 10 students. First one contains an int id, ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    12

    Linked-List fails to load data for 8th node, but loads the other 10. Any clue why?

    I am making a program in C, that creates 2 files for 10 students. First one contains an int id, a char name[10], a char surname[10] and an int year in which a student got into school in each line. The second file contains 3 phones for each student, one in each line with their id in the start. So total lines of second file are 10*3.
    The 2 files are created successfully. Then i am trying to load in 2 different linked lists those files.
    The first linked-list will contain the id,name,surname,year in each node and the pointer to next node.
    The second linked-list will contain the id,tel1[10],tel2[10],tel3[10] of each student in each node and the pointer to next node.
    All the variables are random generated except the id which is serial.
    The first linked is loaded successfully for the 10 students. The problem is in the second list. It seems to work correctly, because it loads the first 7 lines of the file (phone.txt), but in the 8th the telephones that loads are three 28 or 30digits numbers, insted of 10. 9th and 10th are loaded successfully. I can't figure out why 8th is not loaded right. The proccess is the same for all of them. Here is the code: (i had to post it all because first time i see a problem like that and i don't have any clue from where it may comes from.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stddef.h>
    #include <time.h>
    
    #define size 10
    
    typedef struct student_node{
            int id;
            char name[10],surname[10];
            int year;
            struct student_node *next;
    }*stnode;
    
    typedef struct telephone_node{
            int id;
            int tel1[10],tel2[10],tel3[10];
            struct telephone_node *next;
    }*telnode;
    
    void printStList(stnode *StList){    //printing the student list
         
         stnode t;
         
         t=(*StList);
         if(t==NULL){
                           printf("Empty List\n");
         }
         else{
             while(t!=NULL){
                        printf("%d ",t->id);
                        printf("%s ",t->name);
                        printf("%s ",t->surname);
                        printf("%d\n",t->year);
                        t=t->next;
             }
         }
    }
    
    void printTel(int tel[]){     //prints an int 10[] array
                 
                 int i;
                 
                 for(i=0;i<10;i++){
                                   printf("%d",tel[i]);
                 }
                 printf(" ");
    }
    
    void printTelList(telnode *TelList){     //prints the phone list
         
         telnode t;
         
         t=(*TelList);
         if(t==NULL){
                           printf("Empty List\n");
         }
         else{
             while(t!=NULL){
                        printf("%d ",t->id);
                        printTel(t->tel1);
                        printTel(t->tel2);
                        printTel(t->tel3);
                        printf("\n");
                        t=t->next;
             }
         }
    }
    
    void TeltoFile(int tel[], FILE *fp2){       //writes an int[10] to file
               int i;
               for(i=0;i<10;i++){
                                 fprintf(fp2,"%d",tel[i]);
               }
               fprintf(fp2,"\n");
    }
                 
    void createFiles(){    //creates the 2 files that will be loaded to list
         
        char name[10], surname[10], temp;
        int i, j, id, year, tel1[10], tel2[10], tel3[10];
         
        FILE *fp1, *fp2;
        fp1=fopen("students.txt","w");
        fp2=fopen("phones.txt","w");
        srand(time(NULL));
        for(i=1; i<=size; i++){
                 id = i;
                 year = rand()%18 + 1990;  //year need to be 1990-2007
                 for(j=0; j<10; j++){          //generates 3 random phones
                          tel1[j]=rand()%10;
                          tel2[j]=rand()%10;
                          tel3[j]=rand()%10;
                 }
                 for(j=0; j<9; j++){
                          temp = rand()%26+97;  //Generate Random Char using ASCII Code
                          name[j] = temp;         
                          temp = rand()%26+97;
                          surname[j] = temp;
                 }
                 name[j] = '\0';
                 surname[j] = '\0';
                 fprintf(fp1,"%d %s %s %d\n",id, name, surname, year); //writes the data of the student in the first file
                 fprintf(fp2,"%d ",id);    //writes the id and phones to second file
                 TeltoFile(tel1, fp2);
                 fprintf(fp2,"%d ",id);
                 TeltoFile(tel2, fp2);
                 fprintf(fp2,"%d ",id);
                 TeltoFile(tel2, fp2);
        }
        fclose(fp1);
        fclose(fp2);
    }
    
    void loadOneStudent(stnode *StList,FILE *fp){   //creates a node of student list and loads the data from the first file
         
         stnode sttemp,stcurr;
         
         sttemp = malloc(sizeof(struct student_node));
         
         sttemp->next=NULL;
         fscanf(fp,"%d",&sttemp->id);
         fscanf(fp,"%s",sttemp->name);
         fscanf(fp,"%s",sttemp->surname);
         fscanf(fp,"%d",&sttemp->year);
         
         if((*StList) == NULL){     //add to list
                   *StList=sttemp;
         }
         else{
              stcurr = *StList;
              while(stcurr->next!=NULL)
                                     stcurr=stcurr->next;
              stcurr->next=sttemp;
         }          
    }
    
    void loadOnePhone(telnode *TelList,FILE *fp){  //creates a node of telephone list and reads the data from second file, WHICH IS BEING SUCCESSFULY CREATED BEFORE!
         
         telnode teltemp,telcurr;
         char c;
         int i,j;
         
         teltemp = malloc(sizeof(struct telephone_node));
         
         teltemp->next=NULL;
         
         for(i=0;i<3;i++){   //run 3 times. One for each telephone
                          fscanf(fp,"%d",&teltemp->id);  //first contains the id
                          fgetc(fp);    //ignore the space after ID
                          if(i==0)    //first time read the first phone
                                  for(j=0;j<10;j++){
                                                   fscanf(fp,"%c",&c);
                                                   teltemp->tel1[j]=atoi(&c);
                          }
                          if(i==1)   //second time read the second phone
                                  for(j=0;j<10;j++){
                                                   fscanf(fp,"%c",&c);
                                                   teltemp->tel2[j]=atoi(&c);
                                  }
                          if(i==2)  //third time read the third phone
                                  for(j=0;j<10;j++){
                                                   fscanf(fp,"%c",&c);
                                                   teltemp->tel3[j]=atoi(&c);
                                  }
                          
         }
         
         if ((*TelList)==NULL){         //add to list
                              (*TelList) = teltemp;
         }
         else{
              telcurr = (*TelList);
              while(telcurr->next!=NULL)
                                     telcurr=telcurr->next;
              telcurr->next = teltemp;
         }
    }
    
    int main(int argc, char **argv){
        
        int i;
        stnode StList;   // the first list for students
        telnode TelList; // the second list for phones
        
        StList = NULL;    //initialize
        TelList = NULL;
        
        createFiles();
        
        FILE *fp;
        fp=fopen("students.txt","r");
        for(i=0;i<size;i++)       //create one node at time, so I load one student at time. Size times which are the total amount of students
                        loadOneStudent(&StList,fp);
        fclose(fp);
        fp=fopen("phones.txt","r");
        
        for(i=0;i<size;i++){   //same here, but problem at i=7, 8th node(????)
                   loadOnePhone(&TelList,fp);
        }  
        fclose(fp);
        printStList(&StList);    //print first list (successfully)
        printTelList(&TelList);  //print second list (problem at id=8)
        
        
        system("PAUSE");
    }
    That was for size 10, for different values of size other nodes fail to load and some successfull loads. I have no clue what's happening. Any advice would be helpfull.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    since your phone numbers are 10 characters long, are you leaving room in your declarations of tel1[10] etc for the delimiting 0 at the end of a string?

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Runs on my computer without problems (Linux, gcc 4.5.2):

    Code:
    $ gcc -g -Wall -Wextra -o test test.c
    $ ./test
    1 mbulkqxlt myoskxfqq 2005
    2 zktumliyg kolaeljnh 1997
    3 addejozmv vnhfjexzm 2000
    4 jbgwjvsxx ovfcjspce 1998
    5 kfcxcgdly ulcfhzpbf 1990
    6 girgseoem xciqqnbsw 1990
    7 aamjihhco ymgwggvsq 1990
    8 jeenohkvc ylptwryal 2002
    9 lwmeguvzh zprdjdvjb 2000
    10 yjbmqggsq pkjwyecmt 1999
    1 9485768273 2109252834 2109252834 
    2 0217914564 7583990612 7583990612 
    3 5083821352 3925989656 3925989656 
    4 2504240121 9851493102 9851493102 
    5 9247968796 9335248718 9335248718 
    6 4217760790 0701156540 0701156540 
    7 8546605744 2966325764 2966325764 
    8 9626046893 7637226115 7637226115 
    9 6671661957 2361729289 2361729289 
    10 0952875897 4654371787 4654371787
    Also tried it succesfully with size 30 and size 60.

    Bye, Andreas
    Last edited by AndiPersti; 06-01-2012 at 10:45 AM. Reason: Additional information (size)

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    The telephone numbers are int[10].
    Maybe is the OS that screws up the nodes. Something wrong with pointers and access. I thought that may would work in other OS but i don't have the comort to try it. In Linux worked fine. The point is if i can fix this problem somehow. Also my compiler (DEV-C++) throws an error when i type a node's data, like e.g. sttemp->id. Something about access, but i have an option to continue so i didn't give to much attention to that ( my internet connection went of till then too). Could it be the compiler's problem?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,542
    Where are you outputting tel3?

    Tim S.

    Code:
                 TeltoFile(tel2, fp2);
                 fprintf(fp2,"%d ",id);
                 TeltoFile(tel2, fp2);
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    Thanks that was an error i hadn't notice. But my main problem is not solved yet and i don't think it is a logical neither a syntax error, because the process is the same for all the nodes of the phone. Also whne i change the size eg to 17, node 8 works fine but 16 messes up now. I can't figure out any syntax or logical problem.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,542
    I suggest posting your files ("students.txt" "phones.txt"); because, it make be a data issue.


    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    I posted them for size 10.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,542
    atoi - C++ Reference

    atoi is not designed to convert a single char to an integer.

    Instead of loadOnePhone using

    Code:
    char c;
    Try

    Code:
    char c[2]={0};
    Tim S.

    PS: The data files did not show any problem to me.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    Yes i know that atoi is for char *. The argument of atoi expects a pointer to a string. But if I use the & is like passing a just the first element of an array, which is not an array but a single char. Thanks anyway for the info.
    When you say that the files didn't show any problem to you, did you check something more than just the plain text?

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    2,542
    Quote Originally Posted by Grigoris Savvas View Post
    When you say that the files didn't show any problem to you, did you check something more than just the plain text?
    I ran you program with the file creation turned off to see if it caused an issue.
    I did NOT see any problems.

    FYI: Using atoi the way you doing is NOT defined and could cause issues like you are seeing.

    Tim S.
    dmh2000 likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Quote Originally Posted by Grigoris Savvas View Post
    The telephone numbers are int[10].
    Maybe is the OS that screws up the nodes. Something wrong with pointers and access. I thought that may would work in other OS but i don't have the comort to try it. In Linux worked fine. The point is if i can fix this problem somehow. Also my compiler (DEV-C++) throws an error when i type a node's data, like e.g. sttemp->id. Something about access, but i have an option to continue so i didn't give to much attention to that ( my internet connection went of till then too). Could it be the compiler's problem?
    oops my bad but it won't be the OS or compiler.

  13. #13
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    Thank a lot Tim. I changed the char c to char c[2] and stored the character in c[1], passing c to atoi. The problem is fixed and all the nodes were loaded correctly. Maybe i should give the tihngs as requested and not mess with the system (especially windows). Thanks anyone who spent his/her time on this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-04-2010, 01:18 PM
  2. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  3. Linked List Node Deletion based on Data
    By gvkalra in forum C++ Programming
    Replies: 3
    Last Post: 09-12-2008, 08:15 AM
  4. Replies: 11
    Last Post: 04-17-2008, 02:29 AM
  5. Replies: 5
    Last Post: 10-04-2001, 03:42 PM

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