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

In summary,The author is using the C functions for reading binary files.However, the rest of the program is in C++.The author has tried using the C++ version but the head variable does not change.The author has included an ifstream ctor but the mode flags are not set correctly.The author has tried to open the file after creating an instance of the input stream, but the file is not found.The author has tried using the stream streaming loader but has a bulk grabber for large files.
  • #1
ORF
170
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
  • #2
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:
[code=c]
yada yada yada;
[/code]
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
 
  • #3
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.
 
  • #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.
 
  • #5
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();
 
  • #6
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!
 
  • #7
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
  • #8
@Integrand: thank you for the remark, you're right; I will be more careful next time ;)

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

1. How do I open a binary file in C++ using the fstream library?

To open a binary file in C++ using the fstream library, you can use the ifstream class with the std::ios::binary flag. This will open the file in binary mode, allowing you to read and write binary data.

2. How do I read binary data from a file in C++?

To read binary data from a file in C++, you can use the read() function from the ifstream class. This function takes in a buffer, the size of the buffer, and the number of bytes to read. It will then read the specified number of bytes from the file into the buffer.

3. What is the difference between binary mode and text mode when working with files in C++?

The main difference between binary mode and text mode when working with files in C++ is how the data is interpreted. In binary mode, data is read and written as-is, without any special formatting or conversions. In text mode, data is read and written as human-readable characters, and may undergo formatting or conversions as specified by the system.

4. What are the advantages of using C++11 functions for reading binary files compared to older methods?

C++11 provides new functions, such as read() and write(), that are specifically designed for working with binary data. These functions are more efficient and easier to use compared to older methods, such as using fread() and fwrite() from the C standard library.

5. Are there any precautions I should take when reading binary files in C++?

When reading binary files in C++, it is important to ensure that the data is being read and written correctly. This includes checking for errors during the reading process and handling endianness (the order of bytes in a multi-byte data type). It is also recommended to use binary mode when working with binary files to avoid any unexpected conversions or formatting.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
2
Replies
57
Views
3K
  • Programming and Computer Science
Replies
6
Views
910
  • Programming and Computer Science
3
Replies
75
Views
4K
Replies
10
Views
956
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
32
Views
2K
Back
Top