Thread: Binary file scanning and reading problem

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    16

    Exclamation Binary file scanning and reading problem

    My program prompts the user to enter a file name and
    open the specified file.

    If the file is not found,the file format is not unsigned binary file U or the file size stores more than 64 values
    than the program exits.

    If the file exists and is opened successfully it will display the stored values.

    Any tips on how to read the user inputed file and to check if it exists or not and how to read them, thrown in the deep end here so could use some friendly advice for a beginner programmer, thanks.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Start at C File I/O Tutorial - Cprogramming.com (Lots of good things on this site)

    Look at fopen, _wfopen
    Good example on what to do if it doesn't open

    Don't forget to Googlate fopen, fclose and all things like that.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    16
    Quote Originally Posted by Click_here View Post
    Start at C File I/O Tutorial - Cprogramming.com (Lots of good things on this site)

    Look at fopen, _wfopen
    Good example on what to do if it doesn't open

    Don't forget to Googlate fopen, fclose and all things like that.
    thanks for your help...my main problem at the moment is actually trying to print the data from a .d file. This is just a sample of what i've done so far (i know error recovery methods and stuff are missing - just providing the main overview of trying to work out how to actually print the .d files data) - when executing the program i get 85 10 0 1 2 3 4 5 6 7 8 9 (the output is supposed to be just 0 1 2 3 4 5 6 7 8 9). Have i totally misunderstood the approach of opening/reading the data and printing it or just missed something ? Again, sorry if this seems obvious - a novice programmer.

    Code:
     #include <stdio.h>
    
    #define BUFFSIZE 8
    
    int main(void)
    {
    
        FILE *inBinFile;
        unsigned char buff[BUFFSIZE];
        int x;
        
    
        inBinFile = fopen( "u.d", "rb" );
        
       
        while  ( ( x = fgetc( inBinFile ) ) != EOF )                                                           
                  {                                                         
                       printf( "%d\n", x );                                                            
         
                  }  
        
     return 0;
    
    }
    Last edited by igor; 08-11-2012 at 04:32 AM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What is a .d file?

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    You might try using a binary editor, like bvi, to confirm what "u.d" file actually contains and compare with your results.

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    16
    Quote Originally Posted by rags_to_riches View Post
    What is a .d file?
    i dunno haha, its an assessment and am supposed to open/read the file.

    If the file is opened successfully, the program shall display the following message "U coded data" ( or P coded data )and display the stored values sequentially. The u.d file is a 'test' file thats all i know - and is supposed to print the numbers 0 - 9. I also earlier managed to print "U". So it's possible that the file does contain U (hence the name, u.d) - however, as stated, the assessment states it should print the values 0 - 9 sequentially.

    I guess my main question is, is the code above correct for printing the values in the file (disregard what its supposed to output) ? Might have to address my lecturer otherwise for further advise.
    Last edited by igor; 08-11-2012 at 05:34 AM.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Might have to address my lecturer otherwise for further advise.
    Shouldn't this be your first choice, where he/she knows what you're expected to provide?

    HINT: ASCII

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by igor View Post
    when executing the program i get 85 10 0 1 2 3 4 5 6 7 8 9 (the output is supposed to be just 0 1 2 3 4 5 6 7 8 9). Have i totally misunderstood the approach of opening/reading the data and printing it or just missed something?
    You're opening the file in binary mode, thus everything you read are bytes and it's up to you how to interpret them (as text, image data, sound data, ...)

    I guess, the first two bytes (85 and 10) should be the header information (85 is the ASCII value for "U" and 10 for "\n" - I think that's the header for "U coded data") and the rest the data you are supposed to read.

    I think the aim of your exercise is to read the first two bytes and depending on their value, interpret the following bytes accordingly.

    All in all you are doing fine with the reading part, it's just a matter of how you interpret the bytes you get.

    Bye, Andreas

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    16
    Quote Originally Posted by AndiPersti View Post
    You're opening the file in binary mode, thus everything you read are bytes and it's up to you how to interpret them (as text, image data, sound data, ...)

    I guess, the first two bytes (85 and 10) should be the header information (85 is the ASCII value for "U" and 10 for "\n" - I think that's the header for "U coded data") and the rest the data you are supposed to read.

    I think the aim of your exercise is to read the first two bytes and depending on their value, interpret the following bytes accordingly.

    All in all you are doing fine with the reading part, it's just a matter of how you interpret the bytes you get.

    Bye, Andreas
    Thank you so much for your answer cleared up a few issues.

  10. #10
    Registered User
    Join Date
    Aug 2012
    Posts
    1
    This is a breach of the universities code of plagiarism, this will be awarded zero marks if found in any part of your assignment.




    Plz meet with me at Figtree Maccas brah we will discuss dis over a B Mac

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in reading binary file(bizzare)
    By dreamvig in forum C Programming
    Replies: 10
    Last Post: 05-07-2012, 03:33 PM
  2. reading binary file problem
    By dreamvig in forum C Programming
    Replies: 6
    Last Post: 04-23-2012, 11:49 AM
  3. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  4. Problem reading a delimited file into a binary tree
    By neolyn in forum C++ Programming
    Replies: 10
    Last Post: 12-09-2004, 07:51 PM
  5. reading binary file
    By Tozilla in forum C Programming
    Replies: 5
    Last Post: 10-25-2003, 07:17 AM