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

  • Context: C/C++ 
  • Thread starter Thread starter maverick_starstrider
  • Start date Start date
  • Tags Tags
    Compressed File Form
Click For Summary

Discussion Overview

The discussion revolves around methods for outputting data to a file in C++ while minimizing file size. Participants explore various techniques and libraries, focusing on the trade-offs between human readability and file compression.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a basic approach to file output using C++ file streams and expresses a need for more space-efficient methods.
  • Another participant suggests using the zlib compression library and mentions gzstream as a C++ stream version, but acknowledges that third-party libraries may not be usable in some contexts.
  • A later reply challenges the necessity of third-party libraries, proposing that writing raw bytes using C++ ostream::write or fwrite could significantly reduce file size, while cautioning about portability issues when transferring files across different systems.

Areas of Agreement / Disagreement

Participants express differing views on the use of third-party libraries for compression, with some advocating for their use while others emphasize the need for alternative methods that do not rely on external dependencies. The discussion remains unresolved regarding the best approach to achieve minimal file size.

Contextual Notes

Some limitations include the potential lack of portability when writing raw bytes and the dependence on specific implementations and compilers, which may restrict the use of certain libraries or methods.

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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K