4

I am trying to learn C++ by writing some code by my own and very new in this field.

Currently, I am trying to read and write a 64 bit integer file. I write 64 bit integer file in following way:

ofstream odt;
odt.open("example.dat");
for (uint64_t i = 0 ; i < 10000000 ; i++)
odt << i ;

Can anybody help me how to read that 64 bit integer file (one by one) ? So, far examples I have found, that reads line by line, not one by one integer.

Edit:

ofstream odt;
odt.open("example.dat");
for (uint64_t i = 0 ; i < 100 ; i++)
odt << i ;

odt.flush() ;

ifstream idt;
idt.open("example.dat");
uint64_t cur;
while( idt >> cur ) {
  cout << cur ;
}
user1838343
  • 451
  • 2
  • 8
  • 16
  • I give above code for an example. I have added neccessary exception handling with that. – user1838343 Nov 24 '12 at 09:57
  • If you're going to use text, separate your writes with whitespace between each, or you'll quickly find it impossible to read them back as you wrote them out. – WhozCraig Nov 24 '12 at 09:59
  • @WhozCraig Is it not possible to raed it back by 64 bit chunck ? – user1838343 Nov 24 '12 at 10:01
  • 2
    Your integers are being written as *text*, not as 64-bit chunks. That is normal behaviour when writing to a file using `ofstream`. If you want to write the actual binary bits to the file, you'll have to use something other than `<<`. – Greg Hewgill Nov 24 '12 at 10:03
  • @GregHewgill, if I want to write and read the above, what should I do thus file size will be less and I can do read write faster ? – user1838343 Nov 24 '12 at 10:06
  • It is entirely possible to write your values in byte-form (8bytes per value). If you want portability you'll need to ensure to account endian (big or small) configuration. But as Greg said, you'll need to use the lower-level raw functions. – WhozCraig Nov 24 '12 at 10:07
  • 3
    You really need to get a good book on C++ and read it - you can't learn a programming language properly through guesswork + trial and error. – Paul R Nov 24 '12 at 10:08
  • @PaulR, I want some pointers to start. – user1838343 Nov 24 '12 at 10:11
  • 2
    @user1838343: a good book is the best pointer you'll ever get, by far. – Paul R Nov 24 '12 at 10:17

2 Answers2

3

If you must use a text file, you need something to delineate the separation of formatted values. spaces for example:

ofstream odt;
odt.open("example.dat");
for (uint64_t i = 0 ; i < 100 ; i++)
    odt << i << ' ';

odt.flush() ;

ifstream idt;
idt.open("example.dat");
uint64_t cur;
while( idt >> cur )
    cout << cur << ' ';

That being said, I would strongly advise you use lower level iostream methods (write(), read()) and write these in binary.

Sample using read/write and binary data (is there a 64-bit htonl/ntohl equiv btw??)

ofstream odt;
odt.open("example.dat", ios::out|ios::binary);
for (uint64_t i = 0 ; i < 100 ; i++)
{
    uint32_t hval = htonl((i >> 32) & 0xFFFFFFFF);
    uint32_t lval = htonl(i & 0xFFFFFFFF);
    odt.write((const char*)&hval, sizeof(hval));
    odt.write((const char*)&lval, sizeof(lval));
}

odt.flush();
odt.close();

ifstream idt;
idt.open("example.dat", ios::in|ios::binary);
uint64_t cur;
while( idt )
{
    uint32_t val[2] = {0};
    if (idt.read((char*)val, sizeof(val)))
    {
        cur = (uint64_t)ntohl(val[0]) << 32 | (uint64_t)ntohl(val[1]);
        cout << cur << ' ';
    }
}
idt.close();
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • thanks a lot. I am searching google for an hour to find a nice (fast) way to read/write integers to/from a file (need not be a text file) like in 64 bit chunks. But, I haven't find any nice example. What method are you stating, can you give a small example on that ? Sorry, I have very limited idea on C++.. – user1838343 Nov 24 '12 at 12:15
  • @user1838343 if you're unfamiliar with C++ you should go with this for now, but read about the different methods of stream object, specifically the `ostream::write()` method and the `istream::read()` method. It will come to you over time. – WhozCraig Nov 24 '12 at 12:51
  • There is a slightly easier hack then your current method, its faster to. – Garret Gang May 10 '19 at 21:51
  • There exists compiler specific ones for it, ``__builtin_bswap64`` for gcc/clang, ``_byteswap_uint64`` for msvc – visu Feb 18 '21 at 12:21
0

You mean something like this?

ifstream idt;
idt.open("example.dat");
uint64_t cur;
while( idt>>cur ) {
  // process cur
}
Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58