GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Sep-2003, 12:48
calculus87's Avatar
calculus87 calculus87 is offline
Junior Member
 
Join Date: Sep 2003
Location: FLorida
Posts: 41
calculus87 is on a distinguished road

inputing into files


Hello,
Here is my question. I have a program where I have 2 functions. The first function (main) gathers two inputs of type string. I also remove the origional file so as to get rid of any left over text.
The other function is able to take one string at a time and place it into a file. Now the problem is when I run the program and I open up the file, only the last string is in the file, and not both.
Here is the code.

CPP / C++ / C Code:
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
#include <string>
#include "time.h"
#include <vector>
#include <algorithm>
#include <sstream>

void f1(string text)
{
fstream op;
op.open("output.txt",ios:: out);
op<<text<<endl;
op.close();
}

void main()
{
remove("output.txt");
string a,b;
cout<<"enter string 1"<<endl;
cin>>a;
cout<<"enter string 2"<<endl;
cin>>b;
f1(a);
f1(b);
}

I understand that I could make f1 just take 2 strings but I need to do it this way.
Any ideas?

Thanks,
Ronald
__________________
This message is a natural product made from recycled electrons. The slight
variations in spelling and grammar enhance its individual character and
beauty and in no way are to be considered flaws or defects.
  #2  
Old 23-Sep-2003, 03:47
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
The f1() function is reopening the file each time, but the file pointer is at the beginning of the file by default, so it's overwriting what's there each time you write to it.

You can set it up to append data to the end by adding ios::app to the open() function, like so:
CPP / C++ / C Code:
fstream op;
op.open("output.txt",ios:: out | ios::app);

However you are wasting time reopening a file each time you wish to write to it - unless you're worried about file locking - so I'd suggest you leave it open as long as you need it.

GF
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
gxx linker accepts only 7 object files danielxs66 C++ Forum 1 12-Dec-2003 10:27
Opening files with a twist, C++ calculus87 C++ Forum 2 26-Sep-2003 15:41
[Linux] How to upgrade an rpm with source files? JdS Computer Software Forum - Linux 2 18-Jun-2003 10:17
Dedicated to JDS who does not like flash files jrobbio Open Discussion Forum 9 15-Feb-2003 04:24
publishing Drirector/Shockwave files ...? darrin Web Design Forum 1 04-Oct-2002 18:21

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:29.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.