C File Opening Problem

From: Aaron Harvey (spam_at_mehere.com)
Date: 10/29/03


Date: Tue, 28 Oct 2003 23:10:10 -0500

Alright, I'll see if I can explain this clearly enough - as it's
baffling me to no end. I am writing a program for one of my classes that
  requires all C concepts ... so I can't use the easy, streamlined
fstream like I did originally. I had to switch over to the
*FILE celsius;
fscanf (celsius, " %c %f", &temperature_type, &celsius_temperature);

It's unfamiliar to me so I figure there's a pretty good chance for a
screwup. Well ... this is one helluva screwup. I'll include as much code
and explanation as I think will help, if anyone could give me a hand in
this I would be eternally greatful.

Here is the function

/************************************************************
** This Function reads character values and temperature amounts
** from the file celsius.txt and displays them in a meaningful
** manner while counting the invalid and valid lines and passing
** them to the summary function along with the average and sum of
** the temp for the valid lines
**-----------------------------------------------------------
** Parameters Passed: 4: valid_count, invalid_count, celsius_sum
** celsius_avg
**
************************************************************/
void process_celsius(int &valid_count, int &invalid_count, float
&celsius_sum, float &celsius_avg)
{
   valid_count=0; invalid_count=0; celsius_sum=0.0; celsius_avg=0.0;
   int count=0;
   char temperature_type;
   float celsius_temperature, temperature_fahrenheit;
   /* Open file celsius.txt */
   FILE *celsius;
   celsius = fopen("celsius.txt", "r");
   while ((fscanf (celsius, " %c %f", &temperature_type,
&celsius_temperature)) == 2)
   {
      count++;
      fscanf (celsius, " %c %f", &temperature_type,
&celsius_temperature);
      printf("%6c%18.1f%16.1f\n", temperature_type, celsius_temperature,
temperature_fahrenheit);
      if(count == 1)
       page_headers(temperature_type);
      printf("%4d%-5s", count, ".");
     /* Check for temperature compliance if true continue*/
     if((celsius_temperature >= -17.8)&&(celsius_temperature <= 43.3))
     {

      /* Check for character compliance if true continue*/
      if((temperature_type == 'c')||(temperature_type == 'C'))
      {
         temperature_fahrenheit = (celsius_temperature * 0.56) + 32;
         printf("%6c%18.1f%16.1f\n", temperature_type,
celsius_temperature, temperature_fahrenheit);
         valid_count++;
         celsius_sum = celsius_temperature + celsius_sum;
      }//End Character Compliance

      /* else print wrong character */
      else
      {
       printf("%6c%18.1f%16s", temperature_type, celsius_temperature,
mini_dashes);
       printf("%s\n", " Char not C or c");
       invalid_count++;
      }//End Else
     }//End if statement for temp compliance

     /* Else if temperature is not in compliance, print error */
     else
     {
        /* Check to see if error was that temp was lower then -17.8
degrees */
        if(celsius_temperature < -17.8)
        {
           /* If temp was below -17.8 check to see if character was also
wrong */
           if((temperature_type != 'c')&&(temperature_type != 'C'))
           {
              printf("%6c%18.1f%16s", temperature_type,
celsius_temperature, mini_dashes);
              printf("%s\n", " Temp < -17.8 & char not c or C");
              invalid_count++;
           }//End character check if statement

           /* if Character was correct just print temperature error */
           else
           {
              printf("%6c%18.1f%16s", temperature_type,
celsius_temperature, mini_dashes);
              printf("%s\n", " Temp < -17.8");
              invalid_count++;
           }//End else if character was correct
        }//End else if temp was wrong
        /* Check to see if error was that temp was above 43.3 degrees */
        if(celsius_temperature > 43.3)
        {
           /* If Temp was above 43.3 was the character correct? */
           if((temperature_type != 'c')&&(temperature_type != 'C'))
           {
              printf("%6c%18.1f%16s", temperature_type,
celsius_temperature, mini_dashes);
              printf("%s\n", " Temp > 43.3 & char not c or C");
              invalid_count++;
           }//End character check if statement
           /* If character was correct just print temp error */
           else
           {
              printf("%6c%18.1f%16s", temperature_type,
celsius_temperature, mini_dashes);
              printf("%s\n", " Temp > 43.3");
              invalid_count++;
           }//End Temperature error print
        }//End if temp was greater then 43.3 degrees check
    }//End invalid temperature check
   }//End While Statement
   temperature_type = 'c';
   compute_average(fahrenheit_avg, fahrenheit_sum, celsius_avg,
celsius_sum, valid_count, temperature_type);
}/*----------end of process_celsius()-----------*/

*************************************

Here's the file I'm reading from

*************************************

Celsius.txt
c 208.4
C 190.3
c 365.8
e -20.9
C 68.0
c 208.4
c 34.6
c -12.5
C 0.00
c 44.0
x 109.7
c 45.8
C 34.93
c 67.0
c 290.7
C 8.94
C 23.8
c 34.0
w -17.6
c 24.0
c -6.0
c -23.8
C 9.7
c 29.7
C -8.94
A 0.00
c 0.00
C -17.8
C 0.0
C 32.0
p 89.7
c -32.0
c -20.0
c 38.9
C 43.3
c 3.5
c 20.5
C -10.0
f 32.3
z 309.7
c 34.1
c -11.5
C 8.0
c 23.8
u 212.0

********************************
Here's what the output is SUPPOSED to look like (ie the way it looked
when I used fstream
********************************

------------------------------------------------------------------
                 Programmer: Aaron Harvey
                 Class: CSI147 Section 875

                 Project 1 - Celsius/Fahrenheit Processing
                 Due Date: 10/27/2003
------------------------------------------------------------------

     Normallo City Celsius Temperature Information Processed

------------------------------------------------------------------
Reading | Character | Celsius | Converted | Message |
  Number | Read | Temperature | Fahrenheit | (if any) |
------------------------------------------------------------------
    1. c 208.4 --- Temp > 43.3
    2. C 190.3 --- Temp > 43.3
    3. c 365.8 --- Temp > 43.3
    4. e -20.9 --- Temp < -17.8 &
char not c or C
    5. C 68.0 --- Temp > 43.3
    6. c 208.4 --- Temp > 43.3
    7. c 34.6 49.3
    8. c -12.5 25.8
    9. C 0.0 32.0
   10. c 44.0 --- Temp > 43.3
   11. x 109.7 --- Temp > 43.3 & char
not c or C
   12. c 45.8 --- Temp > 43.3
   13. C 34.9 49.5
   14. c 67.0 --- Temp > 43.3
   15. c 290.7 --- Temp > 43.3
   16. C 8.9 36.5
   17. C 23.8 43.9
   18. c 34.0 49.0
   19. w -17.6 --- Char not C or c
   20. c 24.0 44.0
   21. c -6.0 29.0
   22. c -23.8 --- Temp < -17.8
   23. C 9.7 36.8
   24. c 29.7 46.9
   25. C -8.9 27.5
   26. A 0.0 --- Char not C or c
   27. c 0.0 32.0
   28. C -17.8 23.1
   29. C 0.0 32.0
   30. C 32.0 48.0
   31. p 89.7 --- Temp > 43.3 & char
not c or C
   32. c -32.0 --- Temp < -17.8
   33. c -20.0 --- Temp < -17.8
   34. c 38.9 51.5
   35. C 43.3 53.6
   36. c 3.5 33.8
   37. c 20.5 42.3
   38. C -10.0 27.0
   39. f 32.3 --- Char not C or c
   40. z 309.7 --- Temp > 43.3 & char
not c or C
   41. c 34.1 49.0
   42. c -11.5 26.3
   43. C 8.0 36.0
   44. c 23.8 43.9
   45. u 212.0 --- Temp > 43.3 & char
not c or C
------------------------------------------------------------------

                 Number of invalid data lines: 20.

                 Number of valid data lines: 25.

         Sum of Celsius Temperatures on valid lines: 337.0.
         Average of Celsius Temperatures on valid lines: 13.5.

     Normallo City Celsius Temperature Information Processed

---------------------------------------------------------------------
Reading | Character | Fahrenheit | Converted | Message |
  Number | Read | Temperature | Celsius | (if any) |
---------------------------------------------------------------------
    1. F 10.9 -10.6
    2. f 56.4 12.2
    3. F 109.6 38.8
    4. f 0.5 -15.7
    5. f 58.9 13.5
    6. s 500.0 --- Temp > 110.0 &
char not f or F
    7. F 0.0 -16.0
    8. F 6534.0 --- Temp > 110.0
    9. f -6.0 --- Temp < 0.0
   10. F 55.9 12.0
   11. f 101.1 34.5
   12. Z 32.0 --- Char not F or f
   13. F 79.8 23.9
   14. f 4353.0 --- Temp > 110.0
   15. F 94.7 31.3
   16. f 0.0 -16.0
   17. f 94.9 31.5
   18. f 353.0 --- Temp > 110.0
   19. f 212.0 --- Temp > 110.0
   20. f 100.9 34.5
   21. c 194.7 --- Temp > 110.0 &
char not f or F
   22. F 98.4 33.2
   23. f 100.0 34.0
   24. F 32.0 0.0
   25. f -32.0 --- Temp < 0.0
   26. F -212.0 --- Temp < 0.0
   27. f 35.9 2.0
   28. F 110.0 39.0
   29. f 98.4 33.2
   30. F 78.4 23.2
   31. F 58.9 13.5
   32. f 45.7 6.9
   33. f 67.8 17.9
   34. q 109.8 --- Char not F or f
   35. f 54.4 11.2
   36. F 89.9 29.0
   37. F -29.9 --- Temp < 0.0
   38. f 0.6 -15.7
   39. B 49.7 --- Char not F or f
   40. F 98.4 33.2
   41. V 598.7 --- Temp > 110.0 &
char not f or F
   42. c 33.3 --- Char not F or f
   43. f 18.9 -6.5
------------------------------------------------------------------

                 Number of invalid data lines: 15.

                 Number of valid data lines: 28.

         Sum of Fahrenheit Temperatures on valid lines: 1751.4.
         Average of Fahrenheit Temperatures on valid lines: 62.5.

Press any key to continue

*************************************

and this is what it is printing now ...

*************************************

------------------------------------------------------------------
                 Programmer: Aaron Harvey
                 Class: CSI147 Section 875

                 Project 1 - Celsius/Fahrenheit Processing
                 Due Date: 10/27/2003
------------------------------------------------------------------

     Normallo City Celsius Temperature Information Processed

------------------------------------------------------------------
Reading | Character | Celsius | Converted | Message |
  Number | Read | Temperature | Fahrenheit | (if any) |
------------------------------------------------------------------
    1. C 190.3 --- Temp > 43.3
    2. e -20.9 --- Temp < -17.8 &
char not c or C
    3. c 208.4 --- Temp > 43.3
    4. c -12.5 25.0
    5. c 44.0 --- Temp > 43.3
    6. c 45.8 --- Temp > 43.3
    7. c 67.0 --- Temp > 43.3
    8. C 8.9 37.0
    9. c 34.0 51.0
   10. c 24.0 45.4
   11. c -23.8 --- Temp < -17.8
   12. c 29.7 48.6
   13. A 0.0 --- Char not C or c
   14. C -17.8 22.0
   15. C 32.0 49.9
   16. c -32.0 --- Temp < -17.8
   17. c 38.9 53.8
   18. c 3.5 34.0
   19. C -10.0 26.4
   20. z 309.7 --- Temp > 43.3 & char
not c or C
   21. c -11.5 25.6
   22. c 23.8 45.3
   23. u 212.0 --- Temp > 43.3 & char
not c or C
------------------------------------------------------------------

                 Number of invalid data lines: 11.

                 Number of valid data lines: 12.

         Sum of Celsius Temperatures on valid lines: 143.0.
         Average of Celsius Temperatures on valid lines: 11.9.

------------------------------------------------------------------

     Normallo City Celsius Temperature Information Processed

---------------------------------------------------------------------
Reading | Character | Fahrenheit | Converted | Message |
  Number | Read | Temperature | Celsius | (if any) |
---------------------------------------------------------------------
    1. f 56.4 13.7
    2. f 0.5 -17.6
    3. s 500.0 --- Temp > 110.0 &
char not f or F
    4. F 6534.0 --- Temp > 110.0
    5. F 55.9 13.4
    6. Z 32.0 --- Char not F or f
    7. f 4353.0 --- Temp > 110.0
    8. f 0.0 -17.9
    9. f 353.0 --- Temp > 110.0
   10. f 100.9 38.6
   11. F 98.4 37.2
   12. F 32.0 0.0
   13. F -212.0 --- Temp < 0.0
   14. F 110.0 43.7
   15. F 78.4 26.0
   16. f 45.7 7.7
   17. q 109.8 --- Char not F or f
   18. F 89.9 32.4
   19. f 0.6 -17.6
   20. F 98.4 37.2
   21. c 33.3 --- Char not F or f
   22. f 18.9 -7.3
------------------------------------------------------------------

                 Number of invalid data lines: 8.

                 Number of valid data lines: 14.

         Sum of Fahrenheit Temperatures on valid lines: 786.1.
         Average of Fahrenheit Temperatures on valid lines: 56.1.

------------------------------------------------------------------
Press any key to continue



Relevant Pages

  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • compiling problems
    ... char Reprodir; ... float** longitude; ... // allocate memory with error checking for lon and lat files ...
    (comp.lang.c)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... And now a question about something else: why do you use floating ... use then to copy a float into a char *1. ... binary representation doesn't resemble a string like "123.46343" ...
    (comp.lang.c)
  • Re: Floats to chars and chars to floats
    ... I then need a way to take those four char ... > values and construct a float value. ... above code basically treats theValue as an array of unsigned char. ...
    (comp.lang.c)
  • Re: compiling problems
    ... and *.h are located in directory given in the compilation command: ... I have included the code but not the dependencies -- thought ... char Reprodir; ... float** longitude; ...
    (comp.lang.c)