Code:
#include "writelb.h"
int main (int argc, char* argv[]){
FILE *fp,*fp2;
/*checking the number of arguments*/
if(argc != 3) {
printf("Usage : %s <s.i. file> <binary file>\n", argv[0]);
exit(0);
}
/*opening the data file*/
if((fp = fopen(argv[1],"r")) == NULL){
fprintf(stderr, "File %s: open error\n", argv[1]);
exit(1);
}
/*opening the binary file we writing to*/
if((fp2 = fopen(argv[2],"wb")) == NULL){
fprintf(stderr, "File %s: open error\n", argv[2]);
exit(1);
}
char *lines, *guild, *blank;
unsigned char *name, *chartemp;
unsigned int race, classs, level;
FixedLengthRecordPtr newRecord;
FixedLengthPagePtr newPage;
int i;
blank = malloc (sizeof(char)*30);
blank = " ";
/*allocating memory*/
if((lines = malloc(sizeof(char)*50))==NULL ||
(name = malloc(sizeof(char)*12))==NULL ||
(guild = malloc(sizeof(char)*30))==NULL ||
(chartemp = malloc(sizeof(char)*50))==NULL ||
(newPage = malloc(sizeof(FixedLengthPagePtr)))==NULL){
fprintf(stderr,"insufficient memmory");
exit(1);
}
for(i=0 ; i < 11 ; i++){
printf("%d ",i);
if((newPage->data[i] = malloc(sizeof(FixedLengthRecordPtr)))==NULL ){
fprintf(stderr,"insufficient memmory");
exit(1);
}
if((newPage->data[i]->name = malloc(sizeof(char)*12))==NULL ){
fprintf(stderr,"insufficient memmory");
exit(1);
}
if((newPage->data[i]->guild = malloc(sizeof(char)*30))==NULL ){
fprintf(stderr,"insufficient memmory");
exit(1);
}
}
/*reading the lines from the data file "data_2008"*/
int k = 0;
while (fgets(lines,50,fp)!=NULL){
printf("k is %d\n",k);
/*getting rid of the new line char*/
lines = strtok(lines,"\n");
printf("line being tokenised - %s\n",lines);
strcpy(newPage->data[k]->name, strtok(lines,","));
printf("name - %s\n",newPage->data[k]->name);
newPage->data[k]->race = atoi(strtok(NULL,","));
printf("race - %d\n",newPage->data[k]->race );
newPage->data[k]->classs = atoi(strtok(NULL,","));
printf("class - %d\n",newPage->data[k]->classs );
newPage->data[k]->level = atoi(strtok (NULL,","));
printf("level - %d\n",newPage->data[k]->level );
printf("checking guild \n");
if((guild = strtok(NULL,"\0"))!=NULL){
printf("guild is not NULL\n");
strncpy(newPage->data[k]->guild, "test",30);
printf("guild is not NULL after\n");
}
else{
printf("guild is NULL\n");
strcpy(newPage->data[k]->guild, blank);
printf("guild is NULL after\n");
}
/*
printf("%s,%d,%d,%d,%s\n",newPage->data[k]->name,newPage->data[k]->race,newPage->data[k]->classs,newPage->data[k]->level,newPage->data[k]->guild);
*/
for (i = k; i>=0; i--){
printf("here - %s\n",newPage->data[i]->guild);
}
/*k = 3 seg faults for no idea what reason*/
k++;
if (k==11){
k=0;
for (i = 0; i<11; i++){
printf("here -%s",newPage->data[i]->name);
}
}
}
free(lines);
free(name);
free(guild);
free(chartemp);
free(newPage);
fclose(fp);
fclose(fp2);
}
the file included...