tags:

views:

30

answers:

2

I have to code some algorithm's result in a file. A Result is a list of blocks, each one defined by 5 numbers: 3 ints and 2 floats. The simplest way to code it in a file is of course plain text, for example like this:

12 56 81 0.34 1.67 
124 11 76 0.75 6.11 
...

It is also the worst way taking the file size into account.

How could I reduce the file size? If these were all integers, I would just convert them to bytes and simply write them as bytes to file and it would reduce the size a bit. But the floats are more problematic. Any ideas? In C# .NET

A: 

You can always compress the file using a zip library like SharpZipLib.

Oded
not the solution i am looking for
agn
@agn - and yet, from the statement of the question, a valid solution ;). If you have more constraints, please edit your question and add them.
Oded
A: 

Binary variable length would be the most compact (i.e. only store 11 bits for numbers that will only need 11 bits), but this rapidly becomes complex because you need to encode both type and length.

How about storing in text, but compressing (e.g. GZipStream), this will remove almost all (compression isn't perfect) without complex encoding.

Something like:

using (var fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
using (var gz = new GZipStream(fs, CompressionMode.Compress))
using (var writer = new StreamWriter(gz)) {
  foreach (var num in numbers) {
    writer.Write(num);
    writer.Write(' ');
  }
}
Richard
i could do that, i have actually done it before - simple bit coding, i can say for sure that i need max 8 bits for every number (a byte actually) but it's trivial for integers and not so trivial for floats. there were some ways to code floats as bytes but i'm just looking for simpler solution
agn