#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    60
    Rep Power
    12

    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.


    #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. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    The problem is here:
    >> op.open("output.txt",ios:: out);

    You're opening the file in output mode (which overwrites the previous contents). What you want to do is open in append mode.
    op.open("output.txt",ios:: out | ios::append);

    See http://www.developer.com/net/cplus/a...0919_2119781_2 for more on file permissions.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    60
    Rep Power
    12
    hey thanks alot

IMN logo majestic logo threadwatch logo seochat tools logo