Sign in to follow this  
Kryptus

Loading bytes from a file

Recommended Posts

Kryptus    100
I have started learning to use fstream.h and I know how to open up text files, but what I wanted to know is how do I open up a binary file and allocated certain parts to a header structure? Example: struct Header { char Signature[4] char AnotherVariable[4] char Name[25] } The text file would read as this for example: SIGN4302This is a name! So I want the text file to load each part into the header file, so that Signature will equal "SIGN", AnotherVariable will equal "4302", nd Name would equal "This is a name!". I'm sure this has been answered before but it's hard to search for something that I'm unsure of typing in the search field. I hope i'm making sense! Please help me out.

Share this post


Link to post
Share on other sites
snk_kid    1312
this may help out:


#include <cstddef> //for size_t type
#include <fstream>
#include <iostream>

//some useful routines to hide ugliness.
template < typename T >
inline std::istream& readbin(std::istream& in, T& v, size_t size) {
in.read(reinterpret_cast<char*>(&v), size);
return in;

}

template < typename T >
inline std::istream& readbin(std::istream& in, T& v) {
in.read(reinterpret_cast<char*>(&v), sizeof(T));
return in;

}

template < typename T >
inline std::ostream& writebin(std::ostream& out, const T& v, size_t size) {
out.write(reinterpret_cast<const char*>(&v), size);
return out;
}

template < typename T >
inline std::ostream& writebin(std::ostream& out, const T& v) {
out.write(reinterpret_cast<const char*>(&v), sizeof(T));
return out;
}

struct foo_header {
char Signature[4];
char AnotherVariable[4];
char Name[25];
};

std::istream& read_foo_header(std::istream& in, foo_header& h) {
readbin(in, h.Signature);
readbin(in, h.AnotherVariable);
readbin(in, h.Name);
return in;
}

std::ostream& write_foo_header(std::ostream& out, const foo_header& h) {
writebin(out, h.Signature);
writebin(out, h.AnotherVariable);
writebin(out, h.Name);
return out;
}

int main() {

const foo_header out_header = { "sig", "foo", "snk_kid" };
foo_header in_header;

std::ofstream ofs("test.bin", std::fstream::binary);

write_foo_header(ofs, out_header);

ofs.close();

std::ifstream ifs("test.bin", std::fstream::binary);

read_foo_header(ifs, in_header);

std::cout << in_header.Signature << '\n';
std::cout << in_header.AnotherVariable << '\n';
std::cout << in_header.Name << std::endl;

}



note there is not such thing as fstream.h its a pre-standard header, the file is called fstream.

Some may notice that i read & write individual members instead of the whole structure, normally its perfectly fine to do that but some times you get messed up from structure alignment & padding so usually better to just read/write the structure by reading/writing individual members.

note further more sizeof(char) == 1 byte.

[Edited by - snk_kid on November 10, 2004 5:35:52 AM]

Share this post


Link to post
Share on other sites
Endurion    5411
For binary mode use fopen( "filename", "rb" ).

Note the 'b' for binary. When writing use b also.

To read a structure in a block you simply to


Header myHeader;

fread( &myHeader, sizeof( Header ), 1, filehandle );


Important: Take care of struct padding. In your sample case the struct will be padded only at the end, but on different sizes there might be padding in between members.

Share this post


Link to post
Share on other sites
Roboguy    794
Quote:
Original post by Endurion
For binary mode use fopen( "filename", "rb" ).

Note the 'b' for binary. When writing use b also.

To read a structure in a block you simply to


Header myHeader;

fread( &myHeader, sizeof( Header ), 1, filehandle );


Important: Take care of struct padding. In your sample case the struct will be padded only at the end, but on different sizes there might be padding in between members.


that should work too, but he was asking about fstream

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this