LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-04-2006, 08:40 AM   #1
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Rep: Reputation: 18
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
 
Old 04-05-2006, 07:21 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Have you tried in | out ?
 
Old 04-06-2006, 02:54 AM   #3
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Original Poster
Rep: Reputation: 18
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
 
Old 04-06-2006, 05:34 AM   #4
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
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.
 
Old 04-07-2006, 03:24 AM   #5
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Original Poster
Rep: Reputation: 18
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...
 
Old 04-07-2006, 04:05 AM   #6
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
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.
Code:
fstream file(filename, ios::binary | ios::ate | ios::out | ios::in );
Hope that helped
 
Old 04-08-2006, 07:01 AM   #7
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Original Poster
Rep: Reputation: 18
I just tried that and it worked fine, thanks very much,
Gearoid
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
linux CISCO IOS emulator? tekhead2 Linux - Software 2 04-22-2008 03:49 AM
Cisco 871 Router IOS anubis65656 Linux - Networking 1 02-23-2006 11:19 AM
has anyone had trouble with their IOS anubis65656 Linux - Networking 1 02-23-2006 09:49 AM
Ios pk21 Linux - Security 3 08-08-2003 11:13 PM
ios, iostream, fstream & iomanip etc. lsropia Programming 3 03-21-2003 01:50 PM


All times are GMT -5. The time now is 08:11 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration