Thread: Problem with fmax

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Problem with fmax

    Hello there guys,
    I need some help with my program. My program is supposed to collect and return the greater values from two binary files, which contain numbers, and amount of numbers are equal. Then write the values into the third binary file.

    I have two files which contain: 12 34 and 11 35, so it is supposed to collect 12 35. So the problems is that the program only collects the only one number from the end, like only 35 and I have no clue how to fix this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
        FILE *fp, *firstfile, *secondfile;
        //char file_name[260];
        int firstfilenum=0, secondfilenum=0, thirdfilenum=0;
    
    /*  File creation
        int f;
        printf("\nFile name: ");
        scanf("%s", file_name);
        fp = fopen(file_name, "wb");
        printf("Enter information. To end press ctrl+z\n");
        while(!feof(stdin)){
                scanf("%d", &f);
                if (!feof(stdin))
                fwrite(&f, sizeof(int), 1, fp);
        }
        fclose(fp);
    */
    
    firstfile = fopen("1.dat" , "r");
    printf("Numbers on the first file: ");
      while (!feof(firstfile))
         {
        fread(&firstfilenum,sizeof(int),1,firstfile);
        printf("%d ", firstfilenum);
         }
         secondfile = fopen("2.dat" , "r");
         printf("\nNumbers on the second file: ");
         while(!feof(secondfile))
      {
        fread(&secondfilenum,sizeof(int),1,secondfile);
        printf("%d ", secondfilenum);
      }
      fp = fopen("3.dat", "wb");
      while(!feof(fp))
      {
             fscanf(firstfile,"%d",&firstfilenum);
             fscanf(secondfile,"%d",&secondfilenum);
             thirdfilenum = fmax(firstfilenum, secondfilenum);
             {
             fwrite(&thirdfilenum, sizeof(int), 1, fp); }
    
        {
            printf("\nSelected numbers to the third file: ");
           {
            rewind(fp);
            fread(&thirdfilenum,sizeof(int),1,fp);
            printf("%d\n\n", thirdfilenum);
         }
    
         fclose(firstfile);
         fclose(secondfile);
         fclose(fp);
         system("pause");
         return 0;
    }
      }
    }
    Any help would be appreciated, thanks.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Hi Raven. Is there a reason why you're iterating through the files twice? I'm guessing this is a homework assignment requirement?

    It seems like it would be simpler to go through the files a single time, reading from each, comparing, and writing the max to the third file - all within a single loop. It would reduce your program to about 30 lines...

    Check out the simplicity of using getc() for this program.

    Kevin

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Raven_ View Post
    Hello there guys,
    I need some help with my program. My program is supposed to collect and return the greater values from two binary files, which contain numbers, and amount of numbers are equal. Then write the values into the third binary file.

    I have two files which contain: 12 34 and 11 35, so it is supposed to collect 12 35. So the problems is that the program only collects the only one number from the end, like only 35 and I have no clue how to fix this.
    It's not worth fixing. You really need to start over. I'll comment the code so you can see why...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
        FILE *fp, *firstfile, *secondfile;
        //char file_name[260];
        int firstfilenum=0, secondfilenum=0, thirdfilenum=0;
    
    /*  File creation
        int f;
        printf("\nFile name: ");                 // Why are you creating a file???
        scanf("%s", file_name);
        fp = fopen(file_name, "wb");
        printf("Enter information. To end press ctrl+z\n");
        while(!feof(stdin)){                     // Loop until eof, and
                scanf("%d", &f);
                if (!feof(stdin))                  // test for eof?  Why 2 tests?  
                                                   // Make you loop more intelligent
                fwrite(&f, sizeof(int), 1, fp);
        }
        fclose(fp);
    */
    
    firstfile = fopen("1.dat" , "r");               // using fread? Is this a text or
                                                    // a binary file?
    printf("Numbers on the first file: ");
      while (!feof(firstfile))                      // You should already see why 
                                                    // you don't want to use FEOF here....
         {
        fread(&firstfilenum,sizeof(int),1,firstfile); // you read each binary value 
                                                      // and throw it away
        printf("%d ", firstfilenum);
         }
         secondfile = fopen("2.dat" , "r");           // now you do it again with 
                                                      // the second file....
         printf("\nNumbers on the second file: ");
         while(!feof(secondfile))
      {
        fread(&secondfilenum,sizeof(int),1,secondfile);
        printf("%d ", secondfilenum);
      }
      fp = fopen("3.dat", "wb");
      while(!feof(fp))
      {
             fscanf(firstfile,"%d",&firstfilenum);   // Now you read a text value, 
                                                     // but you've already reached EOF!
             fscanf(secondfile,"%d",&secondfilenum); // what value is in your 
                                                     // variable after these reads?
             thirdfilenum = fmax(firstfilenum, secondfilenum);  
                                                     // FMAX is not a standard C 
                                                     // function -- does it exist?
             {
             fwrite(&thirdfilenum, sizeof(int), 1, fp); }
    
        {
            printf("\nSelected numbers to the third file: ");
           {
            rewind(fp);
            fread(&thirdfilenum,sizeof(int),1,fp);
            printf("%d\n\n", thirdfilenum);
         }
    
         fclose(firstfile);
         fclose(secondfile);
         fclose(fp);
         system("pause");
         return 0;
    }
      }
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Hi WaltP. I believe fmax(), fmaxl(), fmaxf(), fmin(), fminl(), and fminf() are all part of C99. Check your man pages (e.g. "man 3 fmax") if you're on any *nix variant. My man page reports that they conform to ISO/IEC 9899:1999(E).

    Meanwhile, Raven, I think WaltP has a point - it might be simpler to scratch the program you've put together so far and start fresh. Consider the following pseudocode as one way to proceed:

    Pseudocode:
    Code:
     open file1 for reading
     open file2 for reading
     open file3 for writing
     
     read num1 from file1
     read num2 from file2
     while not eof file1 AND not eof file2:
         find max of num1 and num2
         write max to file3
         //print num1, num2, max
         read num1 from file1
         read num2 from file2
     endwhile
     
     close file1
     close file2
     close file3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  2. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM