LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
LinkBack Search this Thread
Old 04-24-2006, 08:30 PM   #1
swatward
Member
 
Registered: Jan 2005
Distribution: slackware
Posts: 83

Rep: Reputation: 15
Using C++ to write to a file


This is in windows, but I don't believe it's much different than in linux..

Code:
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("file.txt");
  while (1 == 1)
  {
  myfile << "Writing this to a file.\n";
  myfile.close();
  }
  return 0;
}
Instead of this overwriting "file.txt" every time it runs, i want it to add that line to the next. So if i ran it twice it would be

Writing this to a file.
Writing this to a file.

I'm new to the language and don't know of a way to do it. Thanks.
 
Old 04-24-2006, 08:33 PM   #2
ataraxia
Member
 
Registered: Apr 2006
Location: Pittsburgh
Distribution: Debian Sid AMD64
Posts: 296

Rep: Reputation: 30
Try
Code:
  myfile.open ("file.txt", O_APPEND);
That's how "open" works in plain C - hopefully it does the same in C++.
 
Old 04-25-2006, 12:44 AM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,469

Rep: Reputation: 64
Yea in C thats the lowlevel way i belive.


im sure C++ has something more simpler like fopen for regular C
 
Old 04-25-2006, 04:31 AM   #4
MichaelZ
Member
 
Registered: Mar 2006
Location: Austria
Distribution: Ubuntu Breezy 5.10
Posts: 32

Rep: Reputation: 15
Quote:
Originally Posted by exvor
im sure C++ has something more simpler like fopen for regular C
Hello,

Have a look at here:

http://www.cplusplus.com/ref/iostrea.../ofstream.html

Best wishes,
Michael
 
Old 04-25-2006, 07:01 AM   #5
swatward
Member
 
Registered: Jan 2005
Distribution: slackware
Posts: 83

Original Poster
Rep: Reputation: 15
thanks a ton
 
Old 04-25-2006, 07:45 PM   #6
swatward
Member
 
Registered: Jan 2005
Distribution: slackware
Posts: 83

Original Poster
Rep: Reputation: 15
If anyone wanted to know, this is how i did it.
Code:
#include <iostream>
#include <fstream>
using namespace std;

int main () {
    int x = 1;
  
  while (x != 10000)
  {
        ofstream myfile ("file.txt", ios::app);
  myfile << "Writing this to a file.  " << x  << "\n";
  myfile.close();
  x++;
  }
  return 0;
}
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grip : no write access to write encoded file bidouilleur Linux - Software 5 10-09-2010 09:23 PM
File write Deepak Inbasekaran Programming 1 04-13-2006 02:55 AM
Write to file omerlh Programming 6 10-17-2005 04:02 AM
forrtl: severe (47): write to READONLY file, unit 5, file /dev/pts/1 terrence Programming 1 10-01-2005 10:22 PM
Grip- "no write access to write encoded file" Alvis Linux - Software 4 01-06-2004 04:18 PM


All times are GMT -5. The time now is 09:41 PM.

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