LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   c++ ios::app and ios::ate (http://www.linuxquestions.org/questions/programming-9/c-ios-app-and-ios-ate-431582/)

gearoid_murphy 04-04-2006 08:40 AM

c++ ios::app and ios::ate
 
Hello,
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.

Gearoid

graemef 04-05-2006 07:21 AM

Have you tried in | out ?

gearoid_murphy 04-06-2006 02:54 AM

I'm sorry if i seem a bit sarcastic but yes, yes I have tried the ios::in and ios::out 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

Flesym 04-06-2006 05:34 AM

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:
Code:

#include <fstream>

using namespace std;

int main(){
  const char* filename = "test.txt";
 
  //just build a little sample file
  ofstream sampleFile(filename);
  sampleFile << "This is a sample content\n";
  sampleFile.close();

  //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.seekp(changePos);
  file << "changed text ";
  file.close();
  return 0;
}

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.

gearoid_murphy 04-07-2006 03:24 AM

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...

Flesym 04-07-2006 04:05 AM

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::out' flags. The rest should work.
Code:

fstream file(filename, ios::binary | ios::ate | ios::out | ios::in );
Hope that helped

gearoid_murphy 04-08-2006 07:01 AM

I just tried that and it worked fine, thanks very much,
Gearoid


All times are GMT -5. The time now is 04:52 PM.