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

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
8 replies · 6K views
ORF
Messages
169
Reaction score
19
Hello

I'm using the C functions for reading binary files:
Code:
#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::count << std::hex << head << std::endl;
    }
    fclose(fin);
    /*********/
}

But the rest of the program is in C++. I've tried with

Code:
#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::count << std::hex << head << std::endl;
    }
    fin.close();
/*********/
}

but head doesn't change (I don't know if fread or [count+hex] fails). Is something missing in the C++ code for reading binary files?

Thank you in advance ;)

Greetings
 
Last edited by a moderator:
on Phys.org
ORF said:
Hello

I'm using the C functions for reading binary files:

#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::count << std::hex << head << std::endl;
}​
fclose(fin);
/*********/​
}
To open a binary file, use "rb" in the fopen() call.
Also, when you post code, put it inside code tags, like so:
C:
   yada yada yada;
ORF said:
But the rest of the program is in C++. I've tried with
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.
ORF said:
#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::count << std::hex << head << std::endl;
}​
fin.close();
/*********/​
}

but head doesn't change (I don't know if fread or [count+hex] fails). Is something missing in the C++ code for reading binary files?

Thank you in advance ;)

Greetings
 
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:
If the file cannot be opened, the stream's failbit flag is set.
One possibility is that your code can't find the file "myFile" possibly because it's not in the default directory.
 
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.
 
I usually store the number of elements in the first 8 bytes, so that you know how many to read next.
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();
 
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!
 
ORF said:
@Integrand: that was a mistake in the C++ sample code, sorry; at that point, I should put the "read" fuction (I can't edit).

In my opinion, it is best to post minimal complete code that reproduces the problem. Untested examples can contain additional irrelevant errors.
 
  • Like
Likes   Reactions: Silicon Waffle
@Integrand: thank you for the remark, you're right; I will be more careful next time ;)

Greetings
 
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.
 
  • Like
Likes   Reactions: ORF