Problem:
I wrote a C program that is supposed to loop through an input file and display its tabular contents in an identically laid out table, except, unlike the input file, the values outputted should be in nice evenly tabbed columns.
The text file I'm reading has the following contents:
name surname 123 4.2 f
sara dddd 222 3.2 f
dan ssss 1233 1.3 m
ann aaaa 443 2.2 f
But the output is wrong:
Notice the unwanted line-wrapping, where the value that should be displayed in the last column becomes the first column of the next line.
The program:
int i, j;
char c;
double d;
char str[10];
char t[6]="ssidc"; //Encoded column types descriptor (e.g. string, string, int, double, char"
while(!feof(fa)) {
for(i = 0; i < 5; i++) {
switch(t[i]) {
case('s'):
fscanf(fa,"%s",str);
printf("%s\t",str);
break;
case('c'):
fscanf(fa,"%c",&c);
printf("%c\t",c);
break;
case('d'):
fscanf(fa,"%lf",&d);
printf("%.1lf\t",d);
break;
case('i'):
fscanf(fa,"%d",&j);
printf("%d\t",j);
break;
default:
break;
}
}
printf("\n");//new line
}