I am beginner.I have a file which has lines like MONSTER,ERRTU,14,2 . when i tried to read the lines and store them into a dynamic memory allocated array. it seems like it worked at first but then when i tried to print the elements of it later , it doesnt work properly.Is it about using char * ? How can I fix this problem?
here my code is;
char *lines_ptr;
line_ptr=(char *)malloc(line*(sizeof(char)*100));
int i=0;
if (fptr==NULL){
printf("file could not be opened.");
}
else{
//line=number of lines
while(!feof(fptr)){
for(i=0;i<line;i++){
fscanf(fptr,"%s",lines_ptr+i);
printf("%s\n",(lines_ptr+i));
}
}
printf("%s",(lines_ptr));//this part shows me i did something wrong.
}
here is my output;
HERO,DRIZZT,8,3
HERO,CATTIE,6,3
HERO,BRUENOR,10,1
HERO,WULFGAR,12,4
MONSTER,TROLL,4,3
MONSTER,GOBLIN,1,3
MONSTER,UNDEAD,1,1
MONSTER,VERMIN,3,2
MONSTER,MINDFLAYER,10,2
MONSTER,ERRTU,14,2
HHHHMMMMMMONSTER,ERRTU,14,2
why does it happen?