9 Replies - 432 Views - Last Post: 29 December 2012 - 02:03 PM Rate Topic: -----

#1 Arclite Cham  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-December 12

Problem in file input and output in C programming

Posted 29 December 2012 - 12:36 PM

Hello everyone. Firstly, I am newbie around here as well as in C programming and I am hoping you all can guide me. I have a problem in a code which requires me to convert temperature in fahrenheit into degree celcius which also requires me to use file input and output. Here is my code:


#include <stdio.h>

#define size 15

int main(void)
{
    int high, medium, low, i;
    float fahren[15], celcius[15], sum, averg;
    char grad[16];
    
    FILE *inp, *outp;
    
    inp = fopen("input.txt", "r");
    outp = fopen("output.txt", "w");
    
    for (i=0;i>size;i++) {fscanf(inp,"%f", &fahren[i]);}
    
    for (i=0;i>size;i++) {
        celcius[i] = 5/9 * (fahren[i] - 32);
        sum += celcius[i];}
    
    averg = sum/size;
    
    for (i=0;i>size;i++){
         if (celcius[i] >= 35){grad[i] = 'H'; ++high;}
         else if (celcius[i] < 35 && celcius[i] >= 20){grad[i] = 'M'; ++medium;}
         else {grad[i] = 'L'; ++low;}}
         
    fprintf(outp, "Average of the temperature : %.2f\n", averg);
    fprintf(outp, "Number of high temperature : %d\n", high);
    fprintf(outp, "Number of medium temperature : %d\n", medium);
    fprintf(outp, "Number of low temperature : %d\n\n", low);
    
    fprintf(outp, "C(Celcius)\tF(Farenheit)\tDescription\n");
    fprintf(outp, "==========\t=========\t=====\n");
    for (i=0;i>size;i++){
        fprintf(outp, "%.2f\t%.2f\t%c\n", celcius[i], fahren[i], grad[i]);}

    fclose(inp);
    fclose(outp);
    return 0;
}




There are some problems I am facing here. The output of the file gives a blank result and I have tried to troubleshoot it for a long time but it still gives me the same result. I created the input.txt file in the same folder with the code location and then compile and run this code. Then I keyed in 15 temperatures into the input.txt file and save it. Then I opened the output.txt file and it gives me this:

Average of the temperature : 0.00
Number of high temperature : 2
Number of medium temperature : 40
Number of low temperature : 7417272

C(Celcius) F(Farenheit) Description
========== ========= =====


It seems that the code is having problem in reading the input. Can anyone tell me where the error is?

This post has been edited by macosxnerd101: 29 December 2012 - 12:44 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Problem in file input and output in C programming

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,320
  • Joined: 27-December 08

Re: Problem in file input and output in C programming

Posted 29 December 2012 - 12:46 PM

Remember that 5/9 divides integers, and truncates anything after the decimal place. So 5/9 == 0. You will want to divide 5.0/9, 5/9.0 or 5.0/9.0 instead.
Was This Post Helpful? 1
  • +
  • -

#3 Arclite Cham  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-December 12

Re: Problem in file input and output in C programming

Posted 29 December 2012 - 12:52 PM

Thanks for the reminder. :D However the output still blanks. The average is still 0.00 and the output doesn't print the celcius, fahrenheit and grade arrays.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,320
  • Joined: 27-December 08

Re: Problem in file input and output in C programming

Posted 29 December 2012 - 12:54 PM

That's probably because you test in your loops: i>size. Really, you want i < size. If i > size, your loops will never execute.
Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: Problem in file input and output in C programming

Posted 29 December 2012 - 12:56 PM

You may also want to try printing the values, to the screen, you read from your file to insure that your are reading the file correctly.

You should also check that your files opened correctly.

Is your input file in the same location as your output file?

Jim
Was This Post Helpful? 0
  • +
  • -

#6 Arclite Cham  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-December 12

Re: Problem in file input and output in C programming

Posted 29 December 2012 - 01:02 PM

View Postmacosxnerd101, on 29 December 2012 - 12:54 PM, said:

That's probably because you test in your loops: i>size. Really, you want i < size. If i > size, your loops will never execute.



Thanks for pointing that out! :D Now the code works. Thanks!
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,320
  • Joined: 27-December 08

Re: Problem in file input and output in C programming

Posted 29 December 2012 - 01:03 PM

Glad we could help! :)
Was This Post Helpful? 0
  • +
  • -

#8 Arclite Cham  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-December 12

Re: Problem in file input and output in C programming

Posted 29 December 2012 - 01:03 PM

View Postjimblumberg, on 29 December 2012 - 12:56 PM, said:

You may also want to try printing the values, to the screen, you read from your file to insure that your are reading the file correctly.

You should also check that your files opened correctly.

Is your input file in the same location as your output file?

Jim



Yeah they are located at the same place. :D
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: Problem in file input and output in C programming

Posted 29 December 2012 - 01:22 PM

You should still be checking that your input and output files opened correctly. Just assuming that the files opened leads to some of the biggest problems encountered when doing file input and output. If you open a file always check that it properly opened.

Jim
Was This Post Helpful? 1
  • +
  • -

#10 Arclite Cham  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-December 12

Re: Problem in file input and output in C programming

Posted 29 December 2012 - 02:03 PM

View Postjimblumberg, on 29 December 2012 - 01:22 PM, said:

You should still be checking that your input and output files opened correctly. Just assuming that the files opened leads to some of the biggest problems encountered when doing file input and output. If you open a file always check that it properly opened.

Jim



Okay, I'll keep an eye on it. Thanks for the guidance. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1