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.