Thread: Cant Figure Out File I/O Error!

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    9

    Question Cant Figure Out File I/O Error!

    Objective of this program is to read binary data from a file and print it.

    Code:
    #include <stdio.h>
    
    
    void readFile(void);
    
    
    int main(){
        readFile();
        return 0;
    }
    
    
    void readFile(void){
        FILE *fp;
        char ch;
        if((fp = fopen("newfile","rb"))==NULL){
            printf("Error reading.\n");
            return;
        }
        while(!feof(fp)){
            if(fread(&ch, sizeof ch, 1, fp)!=1){
                printf("Error.\n");
                return;
            }
            putchar(ch);
        }
        fclose(fp);
    }
    But When I Run This Code First It Print "Error." Then Rest Of The File.Say In My File I Have "I AM HUMAN", It Prints "Error. HUMAN"!!
    I Cant Figure Out Whats Wrong In The readFile() Function.It seems right to me.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    n My File I Have "I AM HUMAN"
    Why are you opening the file in binary mode instead of text mode and why are you using fread() instead of fgets()? The description of your file sounds like a text file. How did you write this file? Normally with binary files you must first write the file with unformatted output functions like fwrite() before you try to read it.

    Also what operating system are you using?

    Jim
    Last edited by jimblumberg; 03-14-2013 at 02:37 PM.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    9
    First of all i have used binary mood cause if i used text mood functions like fgets() or fscanf(), i have to use array to catch string from file but i want to make my program dynamic so that it doesn,t depend on file size as array does.I have also assume that i didn,t know file size.if i used array i have to knew that.Another reason for that is its better using binarry mood than text mood as conversion happens when using text mode function.Yes, In description i have used only text as example but its not constant that i will only use text i want to use any type of data.

    Yes, I have written file using fwrite();

    I am using windows.

    Thanks

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    I can't reproduce that "Error." is printed before the content of the file. If I run your program it is printed after the content. Reason: FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    9
    Thanks.
    This Is What I Need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error that I can't figure out.
    By mytrademark in forum C Programming
    Replies: 3
    Last Post: 10-03-2011, 06:54 PM
  2. cant figure out error
    By rozwald in forum C++ Programming
    Replies: 2
    Last Post: 07-08-2010, 02:20 AM
  3. Odd bus error, can't figure it out.
    By Subsonics in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 11:44 AM
  4. can't figure this error out
    By the Wookie in forum C++ Programming
    Replies: 4
    Last Post: 07-08-2003, 03:23 PM
  5. Can't figure it out the error??? HELP!!!
    By xeneize in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2002, 03:44 PM