473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to save int in a more compact way

A int in memory takes 32bits (4 bytes).

But if I use ofstream to save a int like this:

int i=1234567890;
ofstream ofs("c:\\intdata.bin", ios::binary);
ofs<<i;

It becomes 10 bytes in a file.

Is there any way to save a int or double in a more compact way? Thanks.

Nov 22 '06 #1
9 2281
* li********@gmail.com:
A int in memory takes 32bits (4 bytes).
With your compiler.

But if I use ofstream to save a int like this:

int i=1234567890;
ofstream ofs("c:\\intdata.bin", ios::binary);
ofs<<i;

It becomes 10 bytes in a file.
Binary mode doesn't affect the conversion to/from text. It affects the
translation of e.g. end-line markers that's done at a lower level.
You're employing operator<<, and that converts the value to text.

Is there any way to save a int or double in a more compact way?
Yes, but using a textual format is more portable and more accessible to
other tools (not the least text editors), and storage is cheap. It
could matter though for transmission over a network. But even there,
the trend is from binary to textual representations such as XML/SOAP.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '06 #2
linyanhung wrote:
>A int in memory takes 32bits (4 bytes).
On many platforms, yes. On some platforms it's a different bit count.
But if I use ofstream to save a int like this:

int i=1234567890;
ofstream ofs("c:\\intdata.bin", ios::binary);
ofs<<i;

It becomes 10 bytes in a file.
Right. The 'ios::binary' doesn't mean it will be a binary file. It just
means that <<"\n" will not (on some platforms) inexplicably write "\r\n".

So << i just formats the integer as a string.
Is there any way to save a int or double in a more compact way?
Yes, but I won't tell you what it is. Write and read integers as delimited
text, as often as you can, for as many programs as you can before learning
to write binary files.

There is no reason, while you are learning C++, or writing your first
programs, to worry about the size or speed of programs. Google for the term
"premature optimization is the root of all evil", to learn why. Then write
large files that are easy to program.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Nov 22 '06 #3
If you insist in encoding the integer in text like convert 1234 to
"1234" by itoa, maybe you can try BCD to encode the string into the
more concise format.
eg: in BCD, "1234" can be encoded into an array {0x43, 0x21} with two
bytes. Actually, BCD can help you save the half of space.

On 11ÔÂ22ÈÕ, ÏÂÎç1ʱ06·Ö, linyanh...@gmail.com wrote:
A int in memory takes 32bits (4 bytes).

But if I use ofstream to save a int like this:

int i=1234567890;
ofstream ofs("c:\\intdata.bin", ios::binary);
ofs<<i;

It becomes 10 bytes in a file.

Is there any way to save a int or double in a more compact way? Thanks.
Nov 22 '06 #4
HaHaHa Thanks you very much!

So still no one tell me how to do it.

In fact, I need to save more than 250000000 ints in my file. The file
will become too big to handle.

So, is there any one can tell me how to do it? :)

Phlip ¼g¹D¡G
linyanhung wrote:
A int in memory takes 32bits (4 bytes).

On many platforms, yes. On some platforms it's a different bit count.
But if I use ofstream to save a int like this:

int i=1234567890;
ofstream ofs("c:\\intdata.bin", ios::binary);
ofs<<i;

It becomes 10 bytes in a file.

Right. The 'ios::binary' doesn't mean it will be a binary file. It just
means that <<"\n" will not (on some platforms) inexplicably write "\r\n".

So << i just formats the integer as a string.
Is there any way to save a int or double in a more compact way?

Yes, but I won't tell you what it is. Write and read integers as delimited
text, as often as you can, for as many programs as you can before learning
to write binary files.

There is no reason, while you are learning C++, or writing your first
programs, to worry about the size or speed of programs. Google for the term
"premature optimization is the root of all evil", to learn why. Then write
large files that are easy to program.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Nov 22 '06 #5
* li********@gmail.com:
[top-posting]
See FAQ item 5.4.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '06 #6
linyanhung wrote:
HaHaHa Thanks you very much!
Welcome to USENET. ;-)
So still no one tell me how to do it.
In fact, I need to save more than 250000000 ints in my file. The file
will become too big to handle.

Why so many ints? I have been programming for almost 20 years and I never
saved so many.

Oh, yeah, I did for JGA. I invented a file format that stored the difference
between each successive int as a character delta. The ints were expected to
mostly have similar values, and only occassionally differ, so I stored a
stream of deltas, and if any delta were 0xff, the next four bytes were the
entire int.

To stop formatting your ints as strings, start with ofs.write( something &i,
sizeof i);

I forget what something is. It might be a typecast to char *.

When I Google for [ofstream write binary], the very first page is this:

http://www.angelfire.com/country/ald...aryFileIO.html

Its title: C++ Binary File I/O

Dig in!

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Nov 22 '06 #7
Thanks for your answer.

I am now making a model about rice production. I need to calculate and
output data of more than twenty years in 500*500 grids (Something like
GIS). That's why I have so many ints.

Nov 22 '06 #8
linyanhung wrote:
Thanks for your answer.

I am now making a model about rice production. I need to calculate and
output data of more than twenty years in 500*500 grids (Something like
GIS). That's why I have so many ints.
A finite element simulation? I thought you should use a "sparse array" for
that, because the rice ain't everywhere.

But if stomping in a 500^2 grid works, do it!

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Nov 22 '06 #9
li********@gmail.com wrote:
A int in memory takes 32bits (4 bytes).

But if I use ofstream to save a int like this:

int i=1234567890;
ofstream ofs("c:\\intdata.bin", ios::binary);
ofs<<i;

It becomes 10 bytes in a file.

Is there any way to save a int or double in a more compact way?
Yes. You may start by pondering on the following proof of concept. Error
handling is not done. Untested code -- use at your own risk.
#include <cstring>
#include <iostream>
#include <cstddef>

template < typename POD >
class io_converter {

char the_data [ sizeof(POD) ];

friend
std::ostream & operator<< ( std::ostream & o_str,
io_converter const & ic ) {
o_str.write( ic.the_data, sizeof(POD) );
return ( o_str );
}

friend
std::istream & operator>( std::istream & i_str,
io_converter & ic ) {
i_str.read( ic.the_data, sizeof(POD) );
return ( i_str );
}
public:

io_converter ( void )
{
std::memset( &the_data, 0, sizeof(POD) );
}

io_converter ( POD const & data )
{
get( data );
}

void get ( POD const & data ) {
std::memcpy( &the_data, &data, sizeof(POD) );
}

void put ( POD & data ) const {
std::memcpy( &data, &the_data, sizeof(POD) );
}

POD value ( void ) const {
POD result;
put( result );
return ( result );
}

};

#include <fstream>

int main ( void ) {
io_converter<intic;
{
std::ofstream file ( "test.bin", std::ios::binary );
for ( int i = 0; i < 1000; ++i ) {
ic.get( i );
file << ic;
}
}
{
std::ifstream file ( "test.bin", std::ios::binary );
while ( file >ic ) {
std::cout << ic.value() << '\n';
}
}
}

Of course, this is inefficient in that it copies the data in memory around
without a purpose. So alternatively, you may also want to ponder about
this:

#include <cstring>
#include <iostream>
#include <cstddef>

template < typename POD >
class binary_wrapper {

POD & the_data;

friend
std::ostream & operator<< ( std::ostream & o_str,
binary_wrapper const & ic ) {
o_str.write( static_cast<char*>( static_cast<void*>( &ic.the_data ) ),
sizeof(POD) );
return ( o_str );
}

friend
std::istream & operator>( std::istream & i_str,
binary_wrapper & ic ) {
i_str.read( static_cast<char*>( static_cast<void*>( &ic.the_data ) ),
sizeof(POD) );
return ( i_str );
}
public:

binary_wrapper ( POD & data )
: the_data ( data )
{}

POD & value ( void ) {
return ( the_data );
}

POD const & value ( void ) const {
return ( the_data );
}

};

#include <fstream>

int main ( void ) {
{
std::ofstream file ( "test.bin", std::ios::binary );
int i = 0;
binary_wrapper<intbw ( i );
for ( ; i < 1000; ++i ) {
file << bw;
}
}
{
int i;
binary_wrapper<intbw ( i );
std::ifstream file ( "test.bin", std::ios::binary );
while ( file >bw ) {
std::cout << i << '\n';
}
}
}
BTW: you really shouldn't be doing this.
Best

Kai-Uwe Bux
Nov 22 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: James Franklin | last post by:
Hi, I have a number of databases in A2K, written on different machines with different installations of Office. I have found that compacting a database while it is open regularly fails, seemingly...
3
by: Trevor Hughes | last post by:
Hello All I have a database (Access 2000, running on Win 2000), which suffers from bloat over a period of time. In order to solve the problem I set the option to compact on exit. This however...
2
by: Greg Strong | last post by:
Hello All, I've written code in a test database with test data. Everything seems to be working except compact database in VB code per http://www.mvps.org/access/general/gen0041.htm. The reason I...
1
by: robert demo via AccessMonster.com | last post by:
In my startup routine, I have the following code: s = CLng(FileLen(filespec) / 1000000) If s > 5 Then 'FIRST, BACKUP THE FRONT END If BackupFrontEnd = False Then Exit Function End If
6
by: MLH | last post by:
I just used Tools / Database Utilities / Compact Database in Access 97 for the first time. Unlike Access 2.0, it does not ask me to furnish a filename for it to compact into. It just launched...
4
by: Al | last post by:
Hi I need to save lots of images on potentially very large database. Therefore the storage space is very crucial. I need what is most compact way to save an image. Currently I am using something...
5
by: bob | last post by:
Hi Using 2003 - targeting the compact framework (c#), but would like to do most development using the full.net (manually leaving out stuff not in the compact framework). Q. Trying to find a...
6
by: Josetta | last post by:
Access 2003 I've been experiencing some problems with my "monster" database the last couple of days. I imported all objects into a new database yesterday, which pretty much stopped the crashing...
2
by: Karl | last post by:
Using A2000 When I click the save icon in form design, Access closes immediately. No warning messges, nothing. This happens on only one form. I deleted the form and recreated it. I could save...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.