Hello, I am having a very strange problem. I am entering data in a file with the vi editor, and the data is to be read by a C program. Each line of data has to be 80 colums, with spaces to file out the line if there is no more data. Most of the data file is read correctly, but this part of the data is giving me probelms:

P7
1
0.005 0.007
3.150 3.050 2.900 2.850 2.700 2.600 2.550 2.450 2.300
2.150 2.050 1.900 1.800 1.650 1.500 1.400 1.250 1.150
1.050 1.000 0.900 0.850 0.750 0.700 0.650 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 0.000 0.000

I have verified that the lines are space filled, no tabs, to exactly 80 columns. The C code that reads it is as 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 *****/

  fprintf(ff16, "\nLIST OF SOLVENCY SURCHARGE RATES\n");
  if (fscanf(fip, "%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);
  }
  fscanf(fip, "%5s", &djunk);
  if (fscanf(fip, "%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;
  }
  for( int jj = 1; jj <= 7; jj++)
  {
    fscanf(fip, "%5s", &djunk);
  }
  for(i=1;i<=knri+1; i++)
  {
    if (fscanf(fip, "%lf",&solvsurchgrt[i-1]) < 1)
    {
      fprintf(ff16, "Failed to correctly read the screen input for solvency surcharges\n");
    }
    else
    {
      fprintf(ff16, "\n SOLVENCY SURCHARGE RATE = %lf\n", solvsurchgrt[i-1]);
    }
  }

The output from this read is as follows:

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

SOLVENCY SURCHARGE RATE = 3.150000

SOLVENCY SURCHARGE RATE = 3.050000

SOLVENCY SURCHARGE RATE = 2.900000

SOLVENCY SURCHARGE RATE = 2.850000

SOLVENCY SURCHARGE RATE = 2.700000

SOLVENCY SURCHARGE RATE = 2.600000

SOLVENCY SURCHARGE RATE = 2.550000

SOLVENCY SURCHARGE RATE = 2.450000

SOLVENCY SURCHARGE RATE = 2.300000
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges
Failed to correctly read the screen input for solvency surcharges

The value of knri that sets the loop count was read from the same file earlier in the code, and 25 is the correct value, and the loop does do the 25 reads.

Note that I had to put in a read of a dummy value in the first line after the P7( the values of 1 was correctly read), and reads of five dummy values after the two values read in the second line after the P7( the two values in that line were also correctly read). The third line is read correctly, but then the reading of the next two lines, which should bring in the remaining sixteen values, fails to read any values. Since I have confirmed that the lines are 80 columns and space-filled, I do not understand why it is picking up one extra value from the second line shown, five extra values from the third line shown, and then fails completely starting at the fifth line shown. I strongly doubt that there is a problem with the C code I have, the problem is something with the entering of data with vi.

Any help with this is greatly appreciated.

Recommended Answers

All 2 Replies

Sorry, I accidentally entered this thread twice, Please ignore this second submission.

Read each line separately (see man page for getline), and then process the contents of the string read for the line. You really do not want to use fscanf() for this purpose. After you have read the line into a string buffer, you can sscanf() instead to read the contents of that line.

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.