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

In summary, the conversation discusses different methods of outputting data to a file in C++, with a focus on minimizing file size. The use of third party libraries, such as zlib and gzstream, is suggested, but the speaker cannot use them due to limitations in their project. The idea of directly writing raw bytes to the file is also brought up as a solution for reducing file size, with some considerations for portability.
  • #1
maverick_starstrider
1,119
6
Hi, I'm just wondering, usually when I output to file in C++ I just do like:

fout.open("output.txt",ios::eek:ut);
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
  • #2
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:
  • #3
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.
 
  • #4
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.
 

1. How do I output data to a file in the most compressed form using C++?

In order to output data to a file in the most compressed form using C++, you will need to use a library or tool that supports compression, such as zlib or gzip. These libraries provide functions and classes for compressing and decompressing data, and can be incorporated into your C++ code to output compressed files.

2. What is the benefit of outputting data in a compressed form?

The main benefit of outputting data in a compressed form is that it reduces the file size and saves storage space. This can be especially useful when dealing with large amounts of data, as it allows for more efficient storage and transfer of the data.

3. Are there any drawbacks to outputting data in a compressed form?

There are some potential drawbacks to outputting data in a compressed form. One drawback is that it may take longer to compress and decompress the data, which could impact the performance of your program. Additionally, if the compressed file is corrupted or damaged, it may be more difficult to recover the data compared to a non-compressed file.

4. Can I choose the level of compression when outputting data to a file in C++?

Yes, many compression libraries and tools allow you to specify the level of compression when outputting data to a file. The level of compression will affect the trade-off between file size and compression time, so you may need to experiment with different levels to find the best option for your specific needs.

5. Is it possible to output data in a compressed form without using a library or tool?

It is possible to output data in a compressed form without using a library or tool, but it may require more complex coding. You would need to implement your own compression algorithm in C++, which can be time-consuming and may not be as efficient as using a pre-existing library or tool.

Similar threads

  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Programming and Computer Science
Replies
8
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
11
Views
3K
Back
Top