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.