C/C++ Outputting to file in the most compressed form (c++)

AI Thread Summary
When optimizing file output in C++ for minimal space usage, using standard file streams may not be sufficient. The discussion highlights that while common methods like `fout.open` and `fprintf` do not significantly reduce file size, alternative approaches can be more effective. For maximum space efficiency, writing raw bytes using `ostream::write` or `fwrite` is recommended, as this method avoids the overhead of human-readable formats. However, if portability across different systems is a concern, additional steps are necessary to ensure compatibility in data layout. The use of third-party libraries like zlib or gzstream for compression is suggested, but limitations on library usage due to implementation control can restrict options.
maverick_starstrider
Messages
1,118
Reaction score
7
Hi, I'm just wondering, usually when I output to file in C++ I just do like:

fout.open("output.txt",ios::out);
fout.precision(13);
fout << data1 << " " << data2 << " " << data3 << endl;

or something to that effect. i.e. I use c++'s file streams. However, for my current application minimizing space is an absolute must. Therefore, are there other ways of outputting this same data (columns of 3 doubles) to a file that will create a smaller file? I've experimented with fprintf and such but it seems to create the same sized file. Any help is greatly appreciated.
 
Technology news on Phys.org
Use zlib, probably the most common compression library on the planet.

If you want a C++ stream version of it, try gzstream

- Warren
 
Last edited:
chroot said:
Use zlib, probably the most common compression library on the planet.

If you want a C++ stream version of it, try gzstream

- Warren

I can't actually use any third party libraries because I have no control over the implementation, or the compiler.
 
maverick_starstrider said:
I can't actually use any third party libraries because I have no control over the implementation, or the compiler.
Are those really problems?



Anyways, if space really, really is a concern, then you shouldn't be writing anything in human readable text formats, because that is a huge waste of space. Use the C++ ostream::write or the fwrite functions to write raw bytes, e.g.

Code:
double x;
double y[3];
fout.write(static_cast<const char*>(&x), sizeof(double));
fout.write(static_cast<const char*>(y), sizeof(y));

and read similarly. If the file needs to be transferable between different computers that lay things out differently in memory, then you need to do a little more work to write things out in a portable format.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
3
Views
4K
Replies
2
Views
3K
Replies
4
Views
5K
Replies
12
Views
5K
Replies
1
Views
2K
Replies
8
Views
2K
Back
Top