strange outputs using fread

This is a discussion on strange outputs using fread within the C Programming forums, part of the General Programming Boards category; Hi all, This is my first post on this site. I am taking c programming at college and plan on ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    42

    strange outputs using fread

    Hi all,

    This is my first post on this site. I am taking c programming at college and plan on continuing with more advanced program classes.

    I am writing a program that reads from a binary file (if one exists). If not the user can create and write data to one. On the next program run, fread will read from the file and populate an array before going to the fwrite functions. I put a bunch of printf's under the fread so I could test the results of the array. I also have some code commented out that I was toying with, trying to get this to work.

    This is the output that I am getting from my printf tests

    20
    40
    -858993460
    60
    -858993460
    -858993460
    80
    -858993460
    -858993460

    I should be getting: 20,40,60,80,100,120,140,160,180,200

    Here is my code, Is it the fread that is screwing things up? Or is it how the data is written (fwrite)? If it were a text file, I could at least eliminate one option.

    Thanks in advance,

    Michael

    Code:
    void loadArray() {
        int userInput[MAXSIZE] = {0};
        int temp = 0;
        int count = 0;
        int counter=0;
        FILE *binaryPointer;
        
        binaryPointer = fopen("binaryFile.bin", "rb");
        
         if(binaryPointer) {
            
              while(!feof(binaryPointer)) {
                     fread(&count, sizeof(int), 1, binaryPointer);
                     userInput[counter] = count;
                     counter++;
               }
               fclose(binaryPointer);
               printf("%d\n", userInput[0]);
               printf("%d\n", userInput[1]);
               printf("%d\n", userInput[2]);
               printf("%d\n", userInput[3]);
               printf("%d\n", userInput[4]);
               printf("%d\n", userInput[5]);
               printf("%d\n", userInput[6]);
               printf("%d\n", userInput[7]);
               printf("%d\n", userInput[8]);
               pause;
        } 
        else {
               printf("The file does not exist, press any key to create one and write data to it\n");
               pause;
        }
    
    
        binaryPointer = fopen("binaryFile.bin", "ab+");    
                while (temp != -999){
                    printf("Please enter a number to be stored in the array. Enter -999 to quit -   ");
                    scanf("%d", &temp);
                    userInput[counter] = temp;
                    counter++;
                    count++;
                    fwrite(&temp, sizeof(int), count, binaryPointer);
                }
         fclose(binaryPointer);
                
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Code:
    fwrite(&temp, sizeof(int), count, binaryPointer);
    shouldn't that be
    Code:
    fwrite(&temp, sizeof(int), 1, binaryPointer);
    since you are writing one int each time through the loop. as it is, each time through the loop you write the value in temp plus whatever garbage is after temp in memory.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    while(!feof(binaryPointer)) {
    Read: Why it's bad to use feof() to control a loop
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Quote Originally Posted by dmh2000 View Post
    Code:
    fwrite(&temp, sizeof(int), count, binaryPointer);
    shouldn't that be
    Code:
    fwrite(&temp, sizeof(int), 1, binaryPointer);
    since you are writing one int each time through the loop. as it is, each time through the loop you write the value in temp plus whatever garbage is after temp in memory.
    Thank you DMH, I should have tried that earlier. I do have a follow up question though.

    I am getting the correct return now through my printf tests but the last number shows up twice in the array. For example, If I had 10 printf tests than I would get:

    20
    40
    60
    80
    100
    100
    0
    0
    0
    0

    Any idea how I am getting the 2nd 100?

    Thanks again

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by generaltso78 View Post
    Any idea how I am getting the 2nd 100?

    Thanks again
    Read the post just above yours. Are you still using while (!feof(fp)) ? Get rid of that.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Thanks HK and c99. I wasn't thinking properly. Long day. Its all good now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange outputs
    By royroy in forum C Programming
    Replies: 5
    Last Post: 11-15-2012, 11:38 AM
  2. Different outputs on different OS
    By darksifer in forum C Programming
    Replies: 13
    Last Post: 10-19-2010, 01:45 PM
  3. why i getting different outputs?
    By nkrao123@gmail. in forum C Programming
    Replies: 2
    Last Post: 12-08-2009, 07:33 AM
  4. fread returns strange values
    By seaking1 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 02:10 AM
  5. Outputs
    By kas2002 in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2002, 06:12 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21