Thread: hELP:Data Files

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    16

    hELP:Data Files

    there is a problm in this programme
    but i dont know what is it


    question
    Write a C-Program that does the following
    • Read integer numbers from a file inp.dat
    • Calculate the sum, average, and multiplicand of even numbers and store the result and the even numbers in output file out1.dat
    • Calculate the sum, average, and multiplicand of odd numbers and stored the result and the odd numbers in output file out2.dat

    Code:
    #include <stdio.h>
    #include <math.h>
    #include<stdlib.h>
    main ()
    {
       FILE *inp,*outp1,*outp2;
       int  num, even_sum, odd_sum, even_product, odd_product, count_even=0, count_odd=0;
       double even_average, odd_average;
    
                   inp=fopen("C:\\inp.dat","r");
                   outp1=fopen("C:\\out1.txt","w");
                   outp2=fopen("C:\\out2.txt","w");
    if(inp==NULL)
       {
           printf("cannot open file");
           exit(1);
       }
    
    even_sum=0;
    even_product=1;
    odd_sum=0;
    odd_product=1;
    
    
    for(fscanf(inp,"%d",num); num!=EOF; fscanf(inp, "%d", num));
       {
           if(num%2 ==0)
           {
           fprintf(outp1, "even=%d\n", num);
           even_sum +=num;
           even_product *=num;
           count_even++;
           }
    
    
           else
          {
               fprintf(outp2, "odd=%d\n", num);
               odd_sum +=num;
               odd_product *=num ;
               count_odd++;
           }
    
    }
    
           even_average=(even_sum)/(count_even);
           odd_average=(odd_sum)/(count_odd);
    
           fprintf(outp1,"the sum=%d\n the product=%d\nand the average=lf",even_sum,even_product,even_average);
           fprintf(outp2,"the sum=%d\n the product=%d\n and the average=%lf",odd_sum,odd_product,odd_average);
    
    
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It helps to post the error message or description. "there is a problm in this programme" doesn't help too much.

    You probably want to change
    Code:
    even_average=(even_sum)/(count_even);
           odd_average=(odd_sum)/(count_odd);
    to
    Code:
    even_average=((float)even_sum)/(count_even);
           odd_average=((float)odd_sum)/(count_odd);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You also have an extra semicolon here:
    Code:
    for(fscanf(inp,"%d",num); num!=EOF; fscanf(inp, "%d", num));
    Get rid of it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try being helpful once in a while, ok? You don't know what the problem is? Really? I find that hard to believe. How about you actually take some time to describe what's going on? What does it do? What doesn't it do? Damnit, learn to how to ask questions the smart way!


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    in 'for' loop, I don't believe that 'num' get set to EOF
    because when i run the progrmme it doesnot stop

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    Quote Originally Posted by jack999
    in 'for' loop, I don't believe that 'num' get set to EOF
    because when i run the progrmme it doesnot stop
    num shold'nt be set to EOF !!!!
    The return value from scanf , should.

    Also, close all streams you opened.
    This the basic code :
    Code:
    extern int errno;
    
    
    int main(void) 
    {
    const char iname[] = "inp.dat";
    const char out_even_name[] = "out1.txt";
    const char out_odd_name[] = "out2.txt";
    FILE *inp,*outp1,*outp2;
    int  num, even_sum, odd_sum, even_product, odd_product, count_even=0, count_odd=0;
    double even_average, odd_average;
    int    rslt;
    int    reof;
    
    
    rslt = 1;
    if ( ! (inp=fopen(iname,"r")) )
       {
       printf("Failed to open %s - %s\n", iname, strerror(errno));
       rslt = 0;
       }
     
    if ( rslt )
       if ( ! (outp1=fopen(out_even_name,"w") ) )
          {
          printf("Failed to open %s - %s\n", out_even_name, strerror(errno));
          fclose(inp);
          rslt = 0;
          }
    
    if ( rslt )
       if ( ! (outp2=fopen(out_odd_name,"w") ) )
          {
          printf("Failed to open %s - %s\n", out_odd_name, strerror(errno));
          fclose(inp);
          fclose(outp1);
          rslt = 0;
          }
    
    
    
    if ( rslt )
       {
       even_sum=0;
       even_product=1;
       odd_sum=0;
       odd_product=1;
    
       for(reof = fscanf(inp,"%d",&num); reof!=EOF; reof = fscanf(inp, "%d", &num))
          if ( num%2 ==0)
             fprintf(outp1, "even=%d\n", num);
          else
             fprintf(outp2, "odd=%d\n", num);
       }
    
    if ( rslt )
       {
       fclose(inp);
       fclose(outp1);
       fclose(outp2);
       }
    	
    printf("Press enter to end ....\n");
    getchar();
    return(0);
    }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    BTW:
    FAQ > Explanations of... > Why it's bad to use feof() to control a loop
    FAQ > Explanations of... > Definition of EOF and how to use it effectively

    My point: check for success to continue the loop, rather than the lack one of a couple of particular failures to break it. That is,
    Code:
    while ( fscanf(inp, "%d", &num) == 1 )
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by dude543
    Code:
       for(reof = fscanf(inp,"%d",&num); reof!=EOF; reof = fscanf(inp, "%d", &num))
          if ( num%2 ==0)
             fprintf(outp1, "even=%d\n", num);
          else
             fprintf(outp2, "odd=%d\n", num);
       }
    
    
    }

    Have you actually tested that code ( I can guess you didn't due to the missing '{' hehe)? It seems that fscanf() will never return false, it just keeps re-reading the last value. Though, this may be compiler and platform specific - I'm using dec-c++ on XP.

    This case requires the use of the feof() function to test for the EOF

    And no jack, as I said on the PFO boards, I'm not going to tell you how to use it.

    EDIT: never mind the feof() statement, I should read every post thoroughly next time - thanks Dave_Sinkula

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    My point: check for success to continue the loop, rather than the lack one of a couple of particular failures to break it. That is,Code:
    while ( fscanf(inp, "%d", &num) == 1 )

    it worked
    thanks Dave_Sinkula

    and thanks guys for correction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM