Trying hard to find why even after allocating data my program gives o/p as:
#
Hi
Hi
Hi
Hi
and then infinite loop...
Code:
FILE *f;
int i=0;
int j=0;
char ch;
char *data[6];
for(i=0;i<6;i++){
if ((data[i] = (char *) malloc(sizeof(char) * 600)) == NULL)
printf("Error: Null pointer returned\n");
}
clrscr();
if((f=fopen("test.mul","r"))==NULL)
{
printf("Error\n");
}
ch = fgetc(f);
printf("%c\n",ch);
while (!(feof(f))){
for (i=0;i<6;i++){
printf("Hi\n");
do{
ch=fgetc(f) ;
(data[i])[j++] = ch;
} while(ch!='#');
(data[i])[j]='\0';
}
printf("%s",data[0]);
}
fclose(f);
for(i=0;i<6;i++)
free(data[i]);
but a good point is that when i use a simple while !EOF with fgetc, the entire file is being printed as it is.
AK