0

Hello, am working on a program for the U.S. Department of Labor. I am working on two programs for the state of Oregon, and the program that reads data and displays on a screen is working correctly. The code reads tax rates for a specific tax schedule and puts htam in a one-dimensional array. The code in the screen program follows:

            `for (iLine = 0, j = 1; iLine <= 9; ++iLine, j++)
                {
                          if ((iLine * 9) < knri)
                          {
                                strcpy(tempString, "");
                                 for (mm = 0; mm < 5; mm++)
                                 {
                                      strncat(tempString, &range[k][mm], 1);
                                 }
                                  mvwaddstr(win,3,xx,tempString);
                                  k = k + 1;
                                  xx = xx + 11;
                          }
                          mvwprintw(win,yy,3,"%d ",j);
                          yy++;
                          ilast=9;
                          if(iLine==jmax) ilast=imax;
                          /* Read the next line into ppcard, an array at least 81 characters in size. */
                          fgets(ppcard, 82, upd);
                          iOffset = 0;
                          for (iField = 0, i = 0; iField < 9; ++iField, i++)
                          {
                               if ((iLine * 9) + iField > knri)
                               {
                                    break;
                                }
                                else
                                {
                                     if (sscanf(ppcard + iOffset, "%lf%n", &solvsurchgrt[(iLine * 9) + iField], &iNewOffset) < 1)
                                      {
                                           /* stop reading this line and try the next. */
                                            break;
                                      }
                                      else
                                      {
                                           gg = x;
                                           snprintf(surchgrtString, 6, "%5.3f", solvsurchgrt[jj-1]);
                                           for (kk = 0; kk < 5; kk++)
                                            {
                                                 screenChar = surchgrtString[kk];
                                                  mvwaddch(win,y,x,screenChar);
                                                  x = x + 1;
                                            }
                                            x = gg;
                                            /* Add on the length of the last field so that we start reading the next number after that */
                                            iOffset += iNewOffset;
                                            jj++;
                                             y++;
                                            if ( y > 14 )
                                            {
                                                  y = 5;
                                                  x +=11;
                                            }
                                      }
                                }
                             }
                        } /* end READ tax rates */`

The variables that you see in the above code are already declared earlier in the program. This reads in the data without a problem.
The code in the server program for Oregon reads the same data into a two-dimensional array. The reason for the difference is that the
screen program is reading in tax rates for one tax schedule at a time, where the server program reads in all of the tax rates, for all
tax schedules, and uses the second subscript to reference the calculated correct schedule and its associated rates later in the code.
The code in the server program to read in the tax rates follows:

                           `for (l=1; l<=15; l++)
                             {
                                   iFields = 0;
                                   for (i=1; i<=9; i++)
                                   {
                                         fgets(str, 82, fip);
                                         printf("DEBUG4: the line of tax rates read in is %s\n", str);
                                         iOffset = 0;
                                         printf("DEBUG5: iFields is %d\n", iFields);
                                         for (m=1; m<=9; m++)
                                         {
                                              sscanf(str+iOffset, "%5.3f", &xrate[(m-1)+iFields][l-1]);
          DEBUG6: iOffset is %d, xrate is %5.3f, subscript is %d\n", iOffset, xrate[(m-1)+iFields][l-1], (m-1)+iFields);
                                               iOffset += 6;
                                         }
                                         iFields += 9;
                                      }
                                   }`

Once again, the variables used in the above code are declared earlier in the program. The server code above produces the following debug statements:

              DEBUG4: the line of tax rates read in is 0.500  0.60 0     00    800   .900  1.000  1.10 0     00    300       0     0
               DEBUG5: iFields is 0
               DEBUG6: iOffset is 0, xrate is 0.000, subscript is 0
               DEBUG6: iOffset is 6, xrate is 0.000, subscript is 1
               DEBUG6: iOffset is 12, xrate is 0.000, subscript is 2
               DEBUG6: iOffset is 18, xrate is 0.000, subscript is 3
               DEBUG6: iOffset is 24, xrate is 0.000, subscript is 4
               DEBUG6: iOffset is 30, xrate is 0.000, subscript is 5
               DEBUG6: iOffset is 36, xrate is 0.000, subscript is 6
               DEBUG6: iOffset is 42, xrate is 0.000, subscript is 7
               DEBUG6: iOffset is 48, xrate is 0.000, subscript is 8
               DEBUG4: the line of tax rates read in is  0     0     0

               DEBUG5: iFields is 9
               DEBUG6: iOffset is 0, xrate is 0.000, subscript is 9
               DEBUG6: iOffset is 6, xrate is 0.000, subscript is 10
               DEBUG6: iOffset is 12, xrate is 0.000, subscript is 11
               DEBUG6: iOffset is 18, xrate is 0.000, subscript is 12
               DEBUG6: iOffset is 24, xrate is 0.000, subscript is 13
               DEBUG6: iOffset is 30, xrate is 0.000, subscript is 14
               DEBUG6: iOffset is 36, xrate is 0.000, subscript is 15
               DEBUG6: iOffset is 42, xrate is 0.000, subscript is 16
               DEBUG6: iOffset is 48, xrate is 0.000, subscript is 17
               DEBUG4: the line of tax rates read in is 1.400  1.50 0     00    700   .800  1.900  2.00 0     00    200       0     0
                DEBUG5: iFields is 18
                DEBUG6: iOffset is 0, xrate is 0.000, subscript is 18
                DEBUG6: iOffset is 6, xrate is 0.000, subscript is 19
                DEBUG6: iOffset is 12, xrate is 0.000, subscript is 20
                DEBUG6: iOffset is 18, xrate is 0.000, subscript is 21
                DEBUG6: iOffset is 24, xrate is 0.000, subscript is 22
                DEBUG6: iOffset is 30, xrate is 0.000, subscript is 23
                DEBUG6: iOffset is 36, xrate is 0.000, subscript is 24
                DEBUG6: iOffset is 42, xrate is 0.000, subscript is 25
                DEBUG6: iOffset is 48, xrate is 0.000, subscript is 26
                DEBUG4: the line of tax rates read in is  0     0     0

                DEBUG5: iFields is 27
                DEBUG6: iOffset is 0, xrate is 0.000, subscript is 27
                DEBUG6: iOffset is 6, xrate is 0.000, subscript is 28
                DEBUG6: iOffset is 12, xrate is 0.000, subscript is 29
                DEBUG6: iOffset is 18, xrate is 0.000, subscript is 30
                DEBUG6: iOffset is 24, xrate is 0.000, subscript is 31
                DEBUG6: iOffset is 30, xrate is 0.000, subscript is 32
                DEBUG6: iOffset is 36, xrate is 0.000, subscript is 33
                DEBUG6: iOffset is 42, xrate is 0.000, subscript is 34
                DEBUG6: iOffset is 48, xrate is 0.000, subscript is 35
                DEBUG4: the line of tax rates read in is 2.300  2.40 0     00    600   .700  2.800  2.90 0     00    100       0     0
                DEBUG5: iFields is 36
                DEBUG6: iOffset is 0, xrate is 0.000, subscript is 36
                DEBUG6: iOffset is 6, xrate is 0.000, subscript is 37
                DEBUG6: iOffset is 12, xrate is 0.000, subscript is 38
                DEBUG6: iOffset is 18, xrate is 0.000, subscript is 39
                DEBUG6: iOffset is 24, xrate is 0.000, subscript is 40
                DEBUG6: iOffset is 30, xrate is 0.000, subscript is 41
                DEBUG6: iOffset is 36, xrate is 0.000, subscript is 42
                DEBUG6: iOffset is 42, xrate is 0.000, subscript is 43
                DEBUG6: iOffset is 48, xrate is 0.000, subscript is 44
                DEBUG4: the line of tax rates read in is  0     0     0

From the debug output, the counters I declared( iOffset, iFields, and the loop counters) are producing correct values. It is the fgets and
sscanf that do not seem to be working correctly. They do work correctly in the screen program, and I am not sure what the issue is in the server program. A portion of the data being read fron the file follows:

                0.500 0.600 0.700 0.800 0.900 1.000 1.100 1.200 1.300
                1.400 1.500 1.600 1.700 1.800 1.900 2.000 2.100 2.200
                2.300 2.400 2.500 2.600 2.700 2.800 2.900 3.000 3.100
                3.200 3.300 3.400 3.500 3.600 3.700 3.800 3.900 4.000
                4.100 4.200 4.300 4.400 4.500 4.600 4.700 4.800 4.900
                5.000 5.100 5.200 5.300 5.400 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.600 0.700 0.800 0.900 1.000 1.100 1.200 1.300 1.400
                1.500 1.600 1.700 1.800 1.900 2.000 2.100 2.200 2.300
                2.400 2.500 2.600 2.700 2.800 2.900 3.000 3.100 3.200
                3.300 3.400 3.500 3.600 3.700 3.800 3.900 4.000 4.100
                4.200 4.300 4.400 4.500 4.600 4.700 4.800 4.900 5.000
                5.100 5.200 5.300 5.400 5.500 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.700 0.800 0.900 1.000 1.100 1.200 1.300 1.400 1.500
                1.600 1.700 1.800 1.900 2.000 2.100 2.200 2.300 2.400
                2.500 2.600 2.700 2.800 2.900 3.000 3.100 3.200 3.300
                3.400 3.500 3.600 3.700 3.800 3.900 4.000 4.100 4.200
                4.300 4.400 4.500 4.600 4.700 4.800 4.900 5.000 5.100
                5.200 5.300 5.400 5.500 5.600 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.800 0.900 1.000 1.100 1.200 1.300 1.400 1.500 1.600
                1.700 1.800 1.900 2.000 2.100 2.200 2.300 2.400 2.500
                2.600 2.700 2.800 2.900 3.000 3.100 3.200 3.300 3.400
                3.500 3.600 3.700 3.800 3.900 4.000 4.100 4.200 4.300
                4.400 4.500 4.600 4.700 4.800 4.900 5.000 5.100 5.200
                5.300 5.400 5.500 5.600 5.700 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.900 1.000 1.100 1.200 1.300 1.400 1.500 1.600 1.700
                1.800 1.900 2.000 2.100 2.200 2.300 2.400 2.500 2.600
                2.700 2.800 2.900 3.000 3.100 3.200 3.300 3.400 3.500
                3.600 3.700 3.800 3.900 4.000 4.100 4.200 4.300 4.400
                4.500 4.600 4.700 4.800 4.900 5.000 5.100 5.200 5.300
                5.400 5.500 5.600 5.700 5.800 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
                1.000 1.100 1.200 1.300 1.400 1.500 1.600 1.700 1.800
                1.900 2.000 2.100 2.200 2.300 2.400 2.500 2.600 2.700
                2.800 2.900 3.000 3.100 3.200 3.300 3.400 3.500 3.600
                3.700 3.800 3.900 4.000 4.100 4.200 4.300 4.400 4.500
                4.600 4.700 4.800 4.900 5.000 5.100 5.200 5.300 5.400
                5.500 5.600 5.700 5.800 5.900 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

This data represents all the tax rates for six tax schedules, there are rates for fifteen tax schedules in all(every tax schedule can have up to
81 rates, most schedules have fewer rates). Any help with this problem is greatly appreciated.

3
Contributors
2
Replies
25
Views
5 Months
Discussion Span
Last Post by abbott@lantic
0

It is the fgets and sscanf that do not seem to be working correctly.

Garbage in, garbage out. fgets is reading wrong and the scanf code requires that fgets reads correctly in order to do its job. Looking at the very first line, it appears that fgets fails on line 6...

fgets(str, 82, fip);

First line input is 9 values, each leading with a single digit, then a decimal, then another digit, then two zeroes. Total length of line not counting line endings is 70 characters, which is well under 82 characters, so assuming your str buffer in fact is at least 82 characters long, and your fip stream is open for reading correctly, that line should work fine. But the printout suggests it is not fine...

0.500 0.60 0 00 800 .900 1.000 1.10 0 00 300 0 0

It doesn't look anything like the that it was supposed to read in...

            0.500 0.600 0.700 0.800 0.900 1.000 1.100 1.200 1.300

Stop and debug there. Number one, you are not checking the return value of fgets to make sure everything is okay. You need to do that.. See this link and check for a NULL return value.

https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm

If you get a NULL return value, find out why. If NOT, you potentially have some weird non-printable or non-ASCII characters in there. That can happen for a variety of reasons and it stands out since you are saying it works fine except when there is a server involved, so perhaps you are not being delivered ASCII lines. If that ends up being the problem, you can either open up that line in an editor that detects encoding and/or shows non-printable or non-ASCII characters, OR you can loop through str character by character and print out the value of each character and compare them to the ASCII table values and make sure they're all what you think they are.

0

Be very carefull of naming between various systems. I early on learned to use lower case only in my naming of variables, be carefull with special characters. Sometimes if a file-name uses Upper case or mixed case, one system will read it correctly, but a second system may not find that file or create a substitute

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.