Hello, I am having a problem with the fgets statement not reading data correctly. It reads most of the data I need without a problem, but three lines of data are not read correctly. The code in question follows:
strcpy(passw,"P7");
fscanf(fip, "%3s", ckod); /* card # 164 */
fprintf(ff16,"\n\n%12s%12s\n", "card# 164 :", "Password ");
printf("passwd is %s\n", ckod);
if(strcmp(passw,ckod)!=0)
{
printf("\n%64s\n%80s\n", "E R R O R !", "Password is incorrect. Please resubmit.");
exit(164);
}
/***** Cards 165 - 174 *****/
fgets(str, 82, fip);
fprintf(ff16, "\nLIST OF SOLVENCY SURCHARGE RATES\n");
fgets(str, 82, fip);
if (sscanf(&str[1], "%d", &baseYearSurcharge) < 1)
{
fprintf(ff16, "Failed to correctly read the screen input for base year\n");
}
else
{
fprintf(ff16, "\nIS SURCHARGE APPLIED TO THE BASE YEAR ( YES = 1, NO = 0 ) = %d\n", baseYearSurcharge);
}
fgets(str, 82, fip);
if (sscanf(&str[1], "%lf %lf", &solvpctchkinitial, &solvpctchkmaximum) < 2)
{
fprintf(ff16, "Failed to correctly read the screen input for floor and ceiling\n");
}
else
{
fprintf(ff16, "\nINITIAL PERCENTAGE = %lf, MAXIMUM PERCENTAGE = %lf\n", solvpctchkinitial, solvpctchkmaximum);
surchargeCheckPercent = solvpctchkinitial;
}
/* Read tax rates from the file. */
/* Always read 9 lines, with 9 fields each, but only the first (knri) are valid. */
for (iLine = 0; iLine < 9; ++iLine)
{
/* Read the next line into str, an array at least 81 characters in size. */
fgets(str, 82, fip);
fprintf(ff16, "Read in line is %s\n", str); /* DEBUG only, remove when solved */
iOffset = 0;
for (iField = 0; iField < 9; ++iField)
{
if ((iLine * 9) + iField > knri)
{
/* If we are past the last significant rate, stop processing, but keep reading. */
break;
}
else if (sscanf(str + iOffset, "%lf%n", &solvsurchgrt[(iLine * 9) + iField], &iNewOffset) < 1)
{
/* Alert the user that we have bad input in line (iLine + 1) */
fprintf(ff16, "Unable to read a valid number at position %d of line %s.\n", iOffset+1, str);
/* stop reading this line and try the next. */
break;
}
else
{
/* Add on the length of the last field so that we start reading the next number after that */
iOffset += iNewOffset;
fprintf(ff16, "\n SOLVENCY SURCHARGE RATE = %lf\n", solvsurchgrt[jj-1]);
fprintf(ff16, " SURCHARGE RATE IS STORED IN ELEMENT %d OF THE LIST OF SURCHARGES\n", jj);
jj++;
}
}
}
fprintf(ff16, "\n");
The data that this code is reading follows:
P7
1
0.005 0.007
0.02700 0.02625 0.02525 0.02425 0.02325 0.02225 0.02125 0.02025 0.01925
0.01825 0.01725 0.01625 0.01525 0.01425 0.01350 0.01100 0.00725 0.00475
0.00375 0.00275 0.00175 0.00150 0.00150 0.00150 0.00150 0.00100 0.00000
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
card# 164 : Password
LIST OF SOLVENCY SURCHARGE RATES
IS SURCHARGE APPLIED TO THE BASE YEAR ( YES = 1, NO = 0 ) = 1
INITIAL PERCENTAGE = 0.005000, MAXIMUM PERCENTAGE = 0.007000
Read in line is 0.0270 0 25 525 2425 02325 .02225 0.02125 0.0202
SOLVENCY SURCHARGE RATE = 0.027000
SURCHARGE RATE IS STORED IN ELEMENT 1 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 0.000000
SURCHARGE RATE IS STORED IN ELEMENT 2 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 25.000000
SURCHARGE RATE IS STORED IN ELEMENT 3 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 525.000000
SURCHARGE RATE IS STORED IN ELEMENT 4 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 2425.000000
SURCHARGE RATE IS STORED IN ELEMENT 5 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 2325.000000
SURCHARGE RATE IS STORED IN ELEMENT 6 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 0.022250
SURCHARGE RATE IS STORED IN ELEMENT 7 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 0.021250
SURCHARGE RATE IS STORED IN ELEMENT 8 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 0.020200
SURCHARGE RATE IS STORED IN ELEMENT 9 OF THE LIST OF SURCHARGES
Read in line is 0.0270 0 25 525 2425 02325 .0225 0.02125 0.0202
SOLVENCY SURCHARGE RATE = 0.018200
SURCHARGE RATE IS STORED IN ELEMENT 10 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 5.000000
SURCHARGE RATE IS STORED IN ELEMENT 11 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 25.000000
SURCHARGE RATE IS STORED IN ELEMENT 12 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 625.000000
SURCHARGE RATE IS STORED IN ELEMENT 13 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 1525.000000
SURCHARGE RATE IS STORED IN ELEMENT 14 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 1425.000000
SURCHARGE RATE IS STORED IN ELEMENT 15 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 0.013500
SURCHARGE RATE IS STORED IN ELEMENT 16 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 0.011000
SURCHARGE RATE IS STORED IN ELEMENT 17 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 0.007200
SURCHARGE RATE IS STORED IN ELEMENT 18 OF THE LIST OF SURCHARGES
Read in line is 0.0182 5 25 625 1525 01425 .01350 0.01100 0.0072
SOLVENCY SURCHARGE RATE = 0.003700
SURCHARGE RATE IS STORED IN ELEMENT 19 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 5.000000
SURCHARGE RATE IS STORED IN ELEMENT 20 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 75.000000
SURCHARGE RATE IS STORED IN ELEMENT 21 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 175.000000
SURCHARGE RATE IS STORED IN ELEMENT 22 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 150.000000
SURCHARGE RATE IS STORED IN ELEMENT 23 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 150.000000
SURCHARGE RATE IS STORED IN ELEMENT 24 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 0.001500
SURCHARGE RATE IS STORED IN ELEMENT 25 OF THE LIST OF SURCHARGES
SOLVENCY SURCHARGE RATE = 0.001500
SURCHARGE RATE IS STORED IN ELEMENT 26 OF THE LIST OF SURCHARGES
Read in line is 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
Read in line is 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
Read in line is 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
Read in line is 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
Read in line is 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
Read in line is 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
The fgets has no problem reading the last six lines of the nine by nine matrix of data, but cannot read the first three lines of that matrix. It seems that the problem is caused by having five significatn digits to the right of the decimal point, but fgets is reading a character string, so that does make any sense. I am struggling to figure this one out, and could use some help. Please note the following:
The variable str is defined as char str[82] earlier in the code, all variables you see are defined earlier in the code or in a .h file.
I have completely verified that there are no tabs in the lines of data.
I have completely verified that the data is no more than 80 columns( other code enforces that).
I have completely verified that there is a line feed at the end of each line read by fgets.
I have tried putting one space between the fields, and two spaces. It fails with both, but works better with one space.
The P7 is read and used to check that the code is reading from the correct place in the file, but the P7 itself is not printed out.
I need to get this code working properly in this file, and then use it in another file. I appreciate any help I can get in solving this.