Loading text files as binary

Started by
6 comments, last by Zahlman 18 years, 10 months ago
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.
my siteGenius is 1% inspiration and 99% perspiration
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 filefile.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
Something like this:

// include a gazillion appropriate headersusing 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));  }}
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
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]
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]
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;}
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.

This topic is closed to new replies.

Advertisement