std::fstream::seekp( ... std::ios::end )

M

mathieu

Hi there,

I think I misunderstood the purpose of std::fstream::seekp. I would
like to append data at the end of a file. So I wrote the following
piece of code (*).

It prints '0' in my console, which means the call to seekp(0,
std::ios::end) did not seek to the end of the file. How am I supposed
to do that in a portable way ? (no such thing as std::ios::app)

Thanks,

#include <fstream>
#include <iostream>

int main()
{
const unsigned int len = 512;
char buffer1[len] = {};
char buffer2[len] = {};
{
std::eek:fstream of( "dummy.raw", std::ios::binary );
of.write( buffer1, len );
of.close();
}

{
std::eek:fstream of( "dummy.raw", std::ios::binary );
of.seekp( 0, std::ios::end );
std::cout << of.tellp() << std::endl;
of.write( buffer2, len );
of.close();
}

}
 
B

Bo Persson

mathieu said:
Hi there,

I think I misunderstood the purpose of std::fstream::seekp. I would
like to append data at the end of a file. So I wrote the following
piece of code (*).

It prints '0' in my console, which means the call to seekp(0,
std::ios::end) did not seek to the end of the file. How am I
supposed to do that in a portable way ? (no such thing as
std::ios::app)

Thanks,

#include <fstream>
#include <iostream>

int main()
{
const unsigned int len = 512;
char buffer1[len] = {};
char buffer2[len] = {};
{
std::eek:fstream of( "dummy.raw", std::ios::binary );
of.write( buffer1, len );
of.close();
}

{
std::eek:fstream of( "dummy.raw", std::ios::binary );

This opens/creates a new file, as the default for out-files is
std::ios::trunc. You need to make this std::ios::binary|std::ios::app
to append to the existing file.
 
M

mathieu

mathieu said:
Hi there,
I think I misunderstood the purpose of std::fstream::seekp. I would
like to append data at the end of a file. So I wrote the following
piece of code (*).
It prints '0' in my console, which means the call to seekp(0,
std::ios::end) did not seek to the end of the file. How am I
supposed to do that in a portable way ? (no such thing as
std::ios::app)

#include <fstream>
#include <iostream>
int main()
{
const unsigned int len = 512;
char buffer1[len] = {};
char buffer2[len] = {};
{
std::eek:fstream of( "dummy.raw", std::ios::binary );
of.write( buffer1, len );
of.close();
}
{
std::eek:fstream of( "dummy.raw", std::ios::binary );

This opens/creates a new file, as the default for out-files is
std::ios::trunc. You need to make this std::ios::binary|std::ios::app
to append to the existing file.

Thanks. For some reason I was convinced that std::ios::app was not
portable...

Sorry for the noise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top