Thread: repeated value in file

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Question repeated value in file

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    int count=0;
    float data, compare = -9999.0;

    FILE *fptr1, *fptr2;

    if((fptr1 = fopen("hello.dat", "r")) == NULL)
    {
    printf("Error opening file\n");
    exit(1);
    }

    if((fptr2 = fopen("hello1.dat", "w")) == NULL)
    {
    printf("Error opening file1\n");
    exit(1);
    }

    else
    {
    while( !feof(fptr1) )
    {
    fread(&data, sizeof(float), 1, fptr1);

    if(data > compare)
    compare = data;

    fwrite(&data, sizeof(float), 1, fptr2);
    count++;
    }

    fprintf(fptr2, "\n\n%s", "Max float number = ");
    fprintf(fptr2, "%d\n\n", compare);
    fprintf(fptr2, "%s", "No of float values = ");
    fprintf(fptr2, "%d\n", count);

    fclose(fptr1);
    fclose(fptr2);
    }
    return 0;
    }

    Here are my problems:
    I don't know why when 'hello1.dat' was created, the last value of 'hello.dat' was repeated twice? I have ensured that no new line was entered at the last value when creating this 'hello.dat' file.

    I also don't know why it didn't print out the 'compare' value?

    hello.dat is just number of values of float.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > while( !feof(fptr1) )

    Don't use feof. Test the return value of fread instead.
    Code:
    while( fread(&data, sizeof(float), 1, fptr1) > 0 )
    {
        ... stuff goes here ...
    }

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

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Exclamation Mistake.

    Thanks for your help, Quzah!


    >>I also don't know why it didn't print out the 'compare' value?
    Mistake:

    fprintf(fptr2, "%d\n\n", compare);

    The conversion specifier should be %f not %d. Anyway, it still couldn't print the largest float numbers eventhough I had changed it to %f.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Are you sure you're reading a binary file, and writing a text file?

    You might need
    if((fptr1 = fopen("hello.dat", "rb")) == NULL)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM