Jump to content
  • Advertisement
Sign in to follow this  
silverphyre673

Loading text files as binary

This topic is 4721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I'm trying to make a simple file archiving utility (bundling several files into one) similar to tar/winzip, without any compression. I tried loading text files with std::ifstream::binary, but this doesn't appear to work correctly. It is creating a 36k file from two 1k files. I don't think it is a problem of the rest of my code. So can you load text files in this way? If not, what else should I try? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
That should work, alothough my guess is that you're using the stream operators, which will still produce formatted output - even if the file was open in binary.

With out seeing the rest of your code, this is only a guess, but try this:

int myInt = 123456;

std::ofstream file("C:/file.zip", std::ios::binary);

//file << myInt; //This will write "123456" to the file
file.write(reinterpret_cast<const char *>(&myInt), sizeof(myInt)); //This will write the int as it appears in memory, i.e. ' @Ô☺' (only four characters long)

For more information, click me.

HTH

Share this post


Link to post
Share on other sites
Something like this:


// include a gazillion appropriate headers
using namespace std;
void tar(vector<string> infiles, string outfile) {
ofstream out(outfile, ios::binary);
for (vector<string>::iterator it = infiles.begin(); it != infiles.end(); ++i) {
ifstream in(infile, ios::binary);
// Write some other data to "out" to indicate file length, or something
copy(istream_iterator<char>(in),
istream_iterator<char>(),
ostream_iterator(out));
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by desertcube
Actually Zahlman, as Fruny pointed out, using a std::istream_iterator will skip white space, so you should infact use a std::istreambuf_iterator

Wow, I corrected Zahlman [grin] me does happy dance


Well the fact of the matter is if he is just copying a a whole files unmodified there is a much more efficient method than using stream/stream buffer iterators:


#include <string>
#include <fstream>

inline const char* get_str(const char* s) {
return s;
}

template < typename Traits, typename Alloc >
inline const char* get_str(const std::basic_string<char, Traits, Alloc>& s) {
return s.c_str();
}

template < typename InputIterator, typename CharT, typename Traits >
void tar(InputIterator first, InputIterator last,
std::basic_ofstream<CharT, Traits>& out) {

std::basic_ifstream<CharT, Traits> in;

for(; first != last; ++first) {
in.open(get_str(*first));
// ...
out << in.rdbuf();
// ...
in.close();
in.clear();
}

}

#include <cstddef>

int main() {

const char* filenames[] = { "foo.txt", "bar.txt", "foobar.txt" };

const std::size_t N = sizeof(filenames) / sizeof(const char*);

std::ofstream out(...);

tar(filenames, filenames + N, out);

}


[Edited by - snk_kid on June 19, 2005 7:07:43 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by silverphyre673
Hey, I'm trying to make a simple file archiving utility (bundling several files into one) similar to tar/winzip, without any compression.


SimpleArchive [grin]

Share this post


Link to post
Share on other sites
Why doust thau hate thy humble C?! [depressed]

/* This function loads a file into a buffer as binary. */
char *fLoadBinary(char chrFileName[], int intLen)
{
char *bufInfo;
FILE *f;
bufInfo = malloc(intLen);
f = fopen(chrFileName, "rb");
fread(bufInfo,intLen,1,f);
fclose(f);
return bufInfo;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by desertcube
Actually Zahlman, as Fruny pointed out, using a std::istream_iterator will skip white space, so you should infact use a std::istreambuf_iterator

Wow, I corrected Zahlman [grin] me does happy dance


Even reading characters? D: That's not so useful then.

But yes, the rdbuf mapping is teh win.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!