ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I'm appending to a file with a c++ program, at the end of the program, I seekg to the point in the file where I opened it and write the number of records I wrote to the file, unfortunately I am encountering these problems :
if i use ios::app for append operations it will no allow me to reposition the file pointer, making it useless for my purposes.
if I use ios::ate, which should allow me to open a file with the file pointer positioned at the end of the stream, the ios::ate flag acts like an ios::trunc flag and wipes all the previouly written data.
If anyone could shed some light on this behavior it would be illuminating.
I'm sorry if i seem a bit sarcastic but yes, yes I have tried the ios::in and ios:ut flags and every possible combination of them in the ofstream constructors.
Older & more experienced programmers than myself have told be that they never use the c++ fstream objects and always utilise the traditional C interface. Personally I like the c++ tool kit but once the tools start to fail at trivial things like this, then you've got problems
First let's assure, that I understand what you want to do: You want to open an existing file, append some data, then go back to the position where you started to append and write some other data at that position; was that correct so far? If yes, then you first have to know, that you cannot insert new data into the middle of a file, but only overwrite existing data. Here I wrote an example that will do so:
using namespace std;
const char* filename = "test.txt";
//just build a little sample file
sampleFile << "This is a sample content\n";
//declare a new file object for appending and "overwriting"...
fstream file(filename, ios::ate | ios::out | ios::in);
//...mark the current position...
long changePos = file.tellp();
file << "This text will be changed\n";
//..append something else
file << "Append some other text";
//...overwrite previous contents
file << "changed text ";
If you want to "really" insert new data, you have to copy the file into a buffer, modify that buffer and then write it back to the file.
agreed, I had only intended to overwrite (not insert) data at a specfified position, in a manner similar to what you've done in your sample code (much obliged for the demo), however I've tried to do exactly what you've been doing only I also include the ios::binary flag, as I am writing pure binary data.
I don't know if this is the source of the problem, I will investigate...
No, I don't think that the 'ios::banary' flag is a problem. But if you want to overwrite some data of an existing file without blanking it, you have to make sure, that you open the file with both 'ios::in' and 'ios:ut' flags. The rest should work.