LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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-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: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration