I am having a problem with reading input data after checking a position marker in the data file. The code that does the reading follows:

 fip = fopen("fprmf.txt","r");
      .
      .
      .
  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 *****/

  fprintf(ff16, "\nLIST OF SOLVENCY SURCHARGE RATES\n");
  fscanf(fip, "%5.3f", &solvpctchkinitial);
  fscanf(fip, "%5.3f", &solvpctchkmaximum);
  fprintf(ff16, "INITIAL PERCENTAGE = %5.3f, MAXIMUM PERCENTAGE = %5.3f\n", solvpctchkinitial, solvpctchkmaximum);
  surchargeCheckPercent = solvpctchkinitial;
  for(i=1;i<=18; i++)
  {
    fscanf(fip, "%5.3f",&solvsurchgrt[i-1]);
    fprintf(ff16, "\n SOLVENCY SURCHARGE RATE = %5.3f\n", solvsurchgrt[i-1]);
  }
  fprintf(ff16, "\n");

  puts("Finish executing inrate.");

The data that it reads is:

  P7
  0.005   0.007
  3.200   2.850   2.650   2.450   2.250   2.150   2.100   2.000   1.900
  1.800   1.750   1.600   1.500   1.400   1.350   1.300   1.200   1.100

The code calls the P7 a "password", but it is a position marker in the file so the programmer can determine where the data is read and where to put new data.

What I get in the output is:

passwd is P7

and then:

LIST OF SOLVENCY SURCHARGE RATES
INITIAL PERCENTAGE = 0.000, MAXIMUM PERCENTAGE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

SOLVENCY SURCHARGE RATE = 0.000

I have looked at how the data file is buildt, and there is nothing incorrect in the data. I do not know what more to look at to resolve this issue. Any help is greatly appreciated.

There are some issues with fscanf() regarding how it handles bad data if you aren't using it carefully enough. fscanf() returns the number of fields it was able to read and assign to variables. It is important to always check this return value and make sure it is what you expect. This is true of the other versions of this function as well: scanf() and sscanf(). If fscanf() can not read the data from the input stream, the bad data is left there and is still there next time to you try to read it, which will still likely fail.

Lines 17-20: Clearly, the first read failure happened here. Fixing these ones will likely lead to fixing subsequent issues. So, I tried removing the size specifications from your input as they are not needed because your data is delimited by whitespaces. I also combined the two function calls into one function call to simplify validity checking. I added a check to make sure that fscanf() actually read the 2 values. This is assuming that solvpctchkinitial and solvpctchkmaximum are actually floats and not doubles. If they are declared as doubles instead, use "%lf", that is lower-case LF, as the format specifier in your input and output.

fprintf(ff16, "\nLIST OF SOLVENCY SURCHARGE RATES\n");
if (fscanf(fip, "%f %f", &solvpctchkinitial, &solvpctchkmaximum) < 2)
{
    printf("Unable to read rates.  Aborting.\n");
    return(165);
}
fprintf(ff16, "INITIAL PERCENTAGE = %5.3f, MAXIMUM PERCENTAGE = %5.3f\n", solvpctchkinitial, solvpctchkmaximum);

The loop in lines 22-26 suffers from the same problems, so if this fixes things, you can use the same ideas there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.