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

AI Thread Summary
The discussion revolves around reading binary files in C++ using both C and C++ methods. The original poster is attempting to read a binary file containing `uint32_t` integers but encounters issues when switching from C's `fread` to C++'s `ifstream`. Key points include the importance of opening files in binary mode using "rb" for C and ensuring the correct flags are set for C++ file streams. The poster's confusion stems from using `fread` with a `std::ifstream`, which is incorrect since `fread` requires a `FILE*`. Suggestions include using `ifstream::read` properly to read binary data and ensuring the file path is correct. The conversation emphasizes the need for minimal, complete code examples to troubleshoot effectively and acknowledges that traditional C functions remain valid in C++ contexts.
ORF
Messages
169
Reaction score
18
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::cout << 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::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:
Technology news 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::cout << 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::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
 
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 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 ORF
Back
Top