C++ and setting pointer to eof to add records

M

marcia

I am wondering if anyone could help me figure out how to set a pointer
to the eof when trying to add records to existing file in C++?

Any help would be appreciated
 
A

Alf P. Steinbach

* marcia:
I am wondering if anyone could help me figure out how to set a pointer
to the eof when trying to add records to existing file in C++?

Any help would be appreciated

You can seek to the end of the file, and with e.g. ofstream you can specify
that in the constructor.
 
M

mlimber

Alf said:
* marcia:

You can seek to the end of the file, and with e.g. ofstream you can specify
that in the constructor.

Specifically, you can do it like this via the constructor:

#include <fstream>
using namespace std;

int main()
{
ofstream file( "myfile.txt", ios_base::ate );
file << "This text will appear at the end of the file." << endl;
return 0;
}

or like this via the seekp method:

#include <fstream>
using namespace std;

int main()
{
ofstream file( "myfile.txt" );
file.seekp( 0, ios_base::end );
file << "This text will appear at the end of the file." << endl;
return 0;
}

You can get more details at

http://www.cplusplus.com/ref/iostream/fstream/

Cheers! --M
 

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
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top