Thread: Read ints from a binary file that has 2560 numbers in it and write it to a file

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

    Read ints from a binary file that has 2560 numbers in it and write it to a file

    Code:
    #include <stdio.h>
    #include <iostream>
    
    
    using namespace std;
    
    
    // An unsigned char can store 1 Bytes (8bits) of data (0-255)
    typedef unsigned char BYTE;
    
    
    // Get the size of a file
    long getFileSize(FILE *file)
    {
        long lCurPos, lEndPos;
        lCurPos = ftell(file);
        fseek(file, 0, 2);
        lEndPos = ftell(file);
        fseek(file, lCurPos, 0);
        return lEndPos;
    }
    
    
    int main()
    {
        const char *filePath = "/Users/Sarang/Documents/University/Research/aCorn/NIST/Vdg0028.set"; 
        BYTE *fileBuf;          // Pointer to our buffered data
        FILE *file = NULL;      // File pointer
        
        // Open the file in binary mode using the "rb" format string
        // This also checks if the file exists and/or can be opened for reading correctly
        if ((file = fopen("Vdg0028.set", "rb")) == NULL)
            cout << "Could not open specified file" << endl;
        else
            cout << "File opened successfully" << endl;
            
        // Get the size of the file in bytes
        long fileSize = getFileSize(file);
        
        // Allocate space in the buffer for the whole file
        fileBuf = new BYTE[fileSize];
    
    
        // Read the file in to the buffer
        fread(fileBuf, fileSize, 1, file);
        
        // Now that we have the entire file buffered, we can take a look at some binary infomation
        // Lets take a look in hexadecimal
        for (int i = 0; i < 2561; i++)
            printf("%X ", fileBuf[i]);
            
        cin.get();
        delete[]fileBuf;
            fclose(file);
        return 0;
    }
    So this is the code, however i'm still getting some hex values in the output. What am I doing wrong ? also how can i write these values to a text file. All help is appreciated. Thanks

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What am I doing wrong ?
    Mixing C and C++ I/O for one thing. If you're going to write C++ code, then use C++ streams. Choose a language.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    What do you mean "getting some hex"?
    I would hope it's all hex, since you're using "%x" in your printf format.

    I'm not convinced from your other post that you understand exactly what the file format is.

    Consider this example for a single int, stored in two different ways.
    Code:
    #include <stdio.h>
    
    void writeAsText ( int value, const char *filename ) {
      FILE *fp = fopen(filename,"w");
      fprintf(fp,"%d\n",value);
      fclose(fp);
    }
    void writeAsBinary ( int value, const char *filename ) {
      FILE *fp = fopen(filename,"wb");
      fwrite(&value,sizeof(value),1,fp);
      fclose(fp);
    }
    void dumpFile ( const char *filename ) {
      FILE *fp = fopen(filename,"rb");
      int ch;
      printf("Dumping %s\n", filename);
      while ( (ch=fgetc(fp)) != EOF ) {
        printf("%02x\n",ch);
      }
      fclose(fp);
    }
    
    int main ( ) {
      int value = 12345678;
      writeAsText(value,"intAsText.txt");
      writeAsBinary(value,"intAsBinary.bin");
      dumpFile("intAsText.txt");
      dumpFile("intAsBinary.bin");
      return 0;
    }
    Result:
    Code:
    $ gcc foo.c
    $ ./a.out 
    Dumping intAsText.txt
    31
    32
    33
    34
    35
    36
    37
    38
    0a
    Dumping intAsBinary.bin
    4e
    61
    bc
    00
    
    # count the bytes in each file
    $ wc -c intAs*
     4 intAsBinary.bin
     9 intAsText.txt
    13 total
    
    # text is printable
    $ cat intAsText.txt 
    12345678
    
    # hexdump of the files (basically the same output as the program)
    $ odx intAsText.txt 
    000000 31 32 33 34 35 36 37 38 0a                       >12345678.<
    000009
    $ odx intAsBinary.bin 
    000000 4e 61 bc 00                                      >Na..<
    000004
    The text representation of an int is variable in length (depending on the magnitude of the int). But the result is easily human readable, and can be loaded into any editor

    The binary representation on the other hand results in a fixed number of bytes regardless of the value of the integer. You can only "view" these things properly with a hex editor, or a hex dump utility like 'od'. If you try to read them as text, you'll just get garbage and a smattering of random printable characters (like Na) which bear no relationship to the actual integer value.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    16
    Code:
    #include <stdio.h>
    #include <iostream>
    
    
    using namespace std;
    
    
    // Get the size of a file
    long getFileSize(FILE *file)
    {
        long lCurPos, lEndPos;
        lCurPos = ftell(file);
        fseek(file, 0, 2);
        lEndPos = ftell(file);
        fseek(file, lCurPos, 0);
        return lEndPos;
    }
    
    
    int main()
    {
        int j;
        const char *filePath = "xyz"; 
        unsigned int *fileBuf;          // Pointer to our buffered data
        FILE *file = NULL;      // File pointer
        
        // Open the file in binary mode using the "rb" format string
        // This also checks if the file exists and/or can be opened for reading correctly
        
        if ((file = fopen("Vdg0028.set", "rb")) == NULL)
            cout << "Could not open specified file" << endl;
        else
            cout << "File opened successfully" << endl;
    
    
        long fileSize = getFileSize(file)/4;
        
        // Allocate space in the buffer for the whole file
        fileBuf = new unsigned int[fileSize];
    
    
        // Read the file in to the buffer
        fread(fileBuf, fileSize, 1, file);
        
        // Now that we have the entire file buffered, we can take a look at some binary infomation
        
        
        for (int i = 0; i < 2560; i++)
               printf("%u\t", fileBuf[i]);
                   
        cin.get();
        delete[]fileBuf;
        	fclose(file);
        return 0;
    }
    Alright so I got the code to read the file and convert the values to ints. Now I'm having trouble with writing the data to a new text file, although i can write it to a text file from the command line, but i want to do this from the program. Thanks for all the help.

  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Well, what kind of trouble specifically would that be?

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    16
    I want to write the converted values i.e the int values and not the hex values. I'm having trouble with that.

  7. #7
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    That's pretty much what you said before. What I meant is, what is your current code and where exactly are you getting stuck? Or don't you at all know how to go about this?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Maybe it's time to upload / attach a copy of Vdg0028.set for us to look at.
    Also, tell us what you expect the first few numbers ought to be, based on what you understand is in the file (ignore what your code is telling you).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    16
    Quote Originally Posted by antred View Post
    That's pretty much what you said before. What I meant is, what is your current code and where exactly are you getting stuck? Or don't you at all know how to go about this?
    Don't have a code yet, however, I tried to work with fwrite and couldnt figure out what the parameters should be. Logically, however I think that I need to have another pointer that points to the contents of the buffered file and read the ints one at a time.

  10. #10
    Registered User
    Join Date
    Jun 2012
    Posts
    16
    Quote Originally Posted by Salem View Post
    Maybe it's time to upload / attach a copy of Vdg0028.set for us to look at.
    Also, tell us what you expect the first few numbers ought to be, based on what you understand is in the file (ignore what your code is telling you).
    The file size is too big for me to upload it, however, you would ideally expect all the hex values converted to ints and all the ints being printed to the screen (tab separated) however, i want to put all that into a text file. So basically, at least I think, I'll have to use fwrite() to do that. Nonetheless I cant get it to work. I'm thus looking for a few lines of code that could help me use fwrite() in the correct manner.

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    There are no "hex values" being "converted to ints". That's a confused understanding. The file holds a bunch of integers in binary format. In such a format, each byte represents a byte in the internal representation of the integer. This is as opposed to a textual format where each byte (usually) holds a digit of the integer, which could be in decimal, hex, octal, or any numeric base.

    For example, the integer 12345678 (decimal) would take 8 bytes to store in a text file (in decimal format), but would only take 4 bytes in a binary file (assuming 32-bit ints).

    Showing each byte in hex (as is the custom, and is probably what is confusing you), and representing the binary format in little-endian (for simplicity), the decimal integer 12345678 would be :

    Binary file: 0x00 0xBC 0x61 0x4E

    Text file (ascii): 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38

    Note that there is nothing essentially "hexadecimal" about the above. They could just as well be shown in decimal and represent the exact same information:

    Binary file: 0 188 97 78
    Text file (ascii): 49 50 51 52 53 54 55 56

    Now, if you want to write the integers you've read from the binary file to a text file in decimal format, you would use fprintf with %u (assuming you have chosen to use C functions ... which means you should get rid of the C++ stuff, but whatever).
    Last edited by oogabooga; 06-17-2012 at 07:26 PM. Reason: changed printf to fprintf
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > The file size is too big for me to upload it,
    How can 2560 numbers (in any representation) be considered "too large".

    You refuse to upload the file
    You refuse to post a hex dump of part of the file
    You even refuse to even show us the output of your own programs.

    In short, why should we even bother with you any more?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Jun 2012
    Posts
    16
    Quote Originally Posted by Salem View Post
    > The file size is too big for me to upload it,
    How can 2560 numbers (in any representation) be considered "too large".

    You refuse to upload the file
    You refuse to post a hex dump of part of the file
    You even refuse to even show us the output of your own programs.

    In short, why should we even bother with you any more?
    Thanks for all the help I have figured it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read ints from a binary file that has 2560 numbers in it
    By jackofalltrades in forum C Programming
    Replies: 10
    Last Post: 06-15-2012, 08:33 AM
  2. Replies: 3
    Last Post: 08-04-2011, 05:11 PM
  3. Replies: 15
    Last Post: 12-17-2008, 12:11 AM
  4. Read and write binary file?
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2008, 05:31 PM
  5. Copy/Read/Write a binary file(.mp3)
    By swapnaoe in forum C Programming
    Replies: 9
    Last Post: 04-22-2008, 02:38 AM