Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to read a binary file using C++11 functions?

Tags:
  1. Jan 29, 2016 #1

    ORF

    User Avatar

    Hello

    I'm using the C functions for reading binary files:
    Code (C):
    #include <iostream>
    #include <stdio.h>
    void main(){
        /*********/
        uint32_t head=0;
        FILE *fin = NULL;
        fin = fopen("myFile.bin","r");
        while(myCondition){
            fread(&head,4,1,fin);
            std::cout << std::hex << head << std::endl;
        }
        fclose(fin);
        /*********/
    }
    But the rest of the program is in C++. I've tried with

    Code (C):
    #include <fstream>
    #include <ios>
    void main(){
        /*********/
        char head[4];
        std::ifstream fin("myFile",std::ios::binary);
        fin.read((char*) &head,sizeof(head));
        while(myCondition){
            fread(&head,4,1,fin);
            std::cout << std::hex << head << std::endl;
        }
        fin.close();
    /*********/
    }
    but head doesn't change (I don't know if fread or [cout+hex] fails). Is something missing in the C++ code for reading binary files?

    Thank you in advance ;)

    Greetings
     
    Last edited by a moderator: Feb 6, 2016
  2. jcsd
  3. Jan 29, 2016 #2

    Mark44

    Staff: Mentor

    To open a binary file, use "rb" in the fopen() call.
    Also, when you post code, put it inside code tags, like so:
    [code=c]
    yada yada yada;
    [/code]
    I'm a bit rusty on the C++ file I/O. I'm wondering if you need to open the file after creating an instance of your input stream.
     
  4. Jan 29, 2016 #3

    Mark44

    Staff: Mentor

    Regarding the C++ version, here's a link to some docs on the ifstream ctor: http://www.cplusplus.com/reference/fstream/ifstream/ifstream/
    You might not have the mode flags set correctly -- you don't include the in flag.

    From the page in the link:
    One possibility is that your code can't find the file "myFile" possibly because it's not in the default directory.
     
  5. Jan 29, 2016 #4
    Why did you change from uint32_t head to char head[4]? Also, I am surprised that your C++ example compiles, as fread wants a FILE* as its last parameter, not a std::ifstream.
     
  6. Jan 29, 2016 #5
    I usually store the number of elements in the first 8 bytes, so that you know how many to read next.
    Code (C):

    std::ifstream inFile( filePath );

    uint64_t num;
    inFile.read((char*)(&num), sizeof(uint64_t));

    std::vector< double > data( num );
    inFile.read((char*)data.data(), num*sizeof( double ) );

    inFile.close();
     
     
  7. Jan 30, 2016 #6

    ORF

    User Avatar

    Hello

    Thank you for your replies!

    @Jarvis323: your code works fine! In my case, in the binary file there are only integers uint32_t.

    @Integrand: that was a mistake in the C++ sample code, sorry; at that point, I should put the "read" fuction (I can't edit).

    @Mark44: the file is in the directory; the examples of cplusplus.com (about reading and writing binary files) compiled and worked without problems, but it doesn't work with my binaryFile.bin (I don't know how it's built that binaryfile, just that it's a sequence of uint32_t). I know that the binaryFile is fine because I can read it with the C code.

    Thank you for your time! :)

    Greetings!
     
  8. Jan 30, 2016 #7
    In my opinion, it is best to post minimal complete code that reproduces the problem. Untested examples can contain additional irrelevant errors.
     
  9. Jan 30, 2016 #8

    ORF

    User Avatar

    @Integrand: thank you for the remark, you're right; I will be more careful next time ;)

    Greetings
     
  10. Feb 2, 2016 #9
    I usually have two different file loaders, one streaming and one big bulk grabber. Just because something is old C functions doesn't make it wrong for C++. fopen/fseek/fread/fclose are still widely used.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: How to read a binary file using C++11 functions?
Loading...