I started off reading a file with fscanf. I figured I could use fscanf since the file was consistent with just two columns. I got very strange output when using fscanf. Can someone please explain why? When I switched over to fgets with sscanf it worked perfectly. I am curious why though. The only changes I made was switching the fscanf to fgets and adding the sscanf line.
//while (fscanf(trans1_fp, "%c %s", &char_storage, nam) !=EOF)
while(fgets(line1, 80, trans1_fp) != NULL)
{
sscanf(line1, "%c %s", &char_storage, nam);
trans1_char[counter] = char_storage;
trans1_char_table[counter] = malloc(strlen(nam)+1);
strcpy(trans1_char_table[counter], nam);
printf(" trans1_char[counter] is %c \n", trans1_char[counter]);
printf(" trans1_char_table[counter] is %s \n", trans1_char_table[counter]);
printf(" passing through second while loop \n");
counter++;
memset(nam, 0, 80);
char_storage = 0;
//memset(char_storage, 0, 80);
val = 0;
}
This is the output I was getting
trans1_char[counter] is R
trans1_char_table[counter] is XXXX
passing through second while loop
trans1_char[counter] is
trans1_char_table[counter] is R
passing through second while loop
trans1_char[counter] is
trans1_char_table[counter] is DISNEY
passing through second while loop
trans1_char[counter] is
trans1_char_table[counter] is
passing through second while loop
Here is the output I expect.
trans1_char[counter] is R
trans1_char_table[counter] is XXXX
passing through second while loop
trans1_char[counter] is R
trans1_char_table[counter] is DISNEY
passing through second while loop