Sign in to follow this  
Private Donut

Writing to one file from different functions

Recommended Posts

I'm trying to open a file for reading & writing, but I'm having trouble getting different parts of the program to write to the file. I'm hoping to have several standard functions and one class member function write to the same file, but can't seem to write to the file in the class member function using the << operand. I've tried putting arguments in the call to tellall(), but it always gives me an error: invalid operands of types `FILE*' and `const char[26]' to binary `operator<<' Here's a snippet of the basic logic of the code.
#include <iostream>
#include <fstream>

using namespace std;

class Hero{
    public:
    string hname;
    string htype;
    int hstrength;
    Hero(string tname, string ttype, int tstrength);

    void tellall(){
        fout << "Your character's name is " << hname << ".  He is a " << htype;
        fout << " with " << hstrength << " strength.\n";
    }
};

Hero::Hero(string tname, string ttype, int tstrength){
    hname = tname;
    htype = ttype;
    hstrength = tstrength;
}

void functionone(){
    string name, type;
    int strength;
    cout << "\nYou selected function one\n";
    fout << "Thank you for selecting program 1.\n";
    cout << "Create a hero.\n\n";
    cout << "Enter a name: ";
    cin >> name;
    cout << "\nEnter a hero class: ";
    cin >> type;
    cout << "\nEnter a strength: ";
    cin >> strength;
    Hero playerone(name, type, strength);
    playerone.tellall();
}

void functiontwo(){
    cout << "\nYou selected function two\n";
    fout << "Thank you for selecting program 2.\n";
    fout << "Your pre-made character is Bob, he is a wizard with 6 strength";
}

int main(){
    int selection;
    fstream fout;
    fout.open ("Output.dat");
    cout << "Menu\n\n";
    cout << "1. First Item\n";
    cout << "2. Second Item\n\n";
    cout << "Please select program you'd like to run: ";
    cin >> selection;
    if (selection == 1){
        functionone();
    }
    else{
        functiontwo();
    }
    system("pause");
}
[Edited by - Private Donut on April 3, 2006 11:20:12 AM]

Share this post


Link to post
Share on other sites
Hi, please wrap your code in [ source lang="cpp"][/source] or [ code][/code] tags, without the space after [ of course. This will make your code much more readable and people will be more likely to read it.

Share this post


Link to post
Share on other sites
I can't be bothered to read the code, but this is a method of doing it:

1. Create an ofstream object (and open the file and stuff).
2. In the two functions, make that ostream object a parameter (by reference is probably a good idea).
3. Use the said parameter to write to the file.

Simple [smile].

Share this post


Link to post
Share on other sites
Another option is to open the file in append mode. That way you can open different streams in the different functions if you so choose to.

Share this post


Link to post
Share on other sites
It appears that the most obvious problem is that fout is not visible to any function other than main(). tellall(), functionone(), and functiontwo() cannot see the declaration of fout, and thus cannot use the variable in any way.

You need to either pass fout to the functions as parameters, or define fout globally so that it can be used by anything in the source file.

By Parameter:
void functiontwo(fstream& fout){
fout << "Thank you for selecting program 2.\n";
fout << "Your pre-made character is Bob, he is a wizard with 6 strength";
}

int main(){
fstream fout;
fout.open ("Output.dat");
functiontwo(fout);
system("pause");
return 0;
}




Global:
fstream fout;

void functiontwo(){
fout << "Thank you for selecting program 2.\n";
fout << "Your pre-made character is Bob, he is a wizard with 6 strength";
}

int main(){
fout.open ("Output.dat");
functiontwo();
system("pause");
return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
in the global, shouldn't functiontwo have no parameters?

Heh, yeah, thanks for spotting that. I'll edit it. Yay for copy-paste errors.

Share this post


Link to post
Share on other sites
Quote:
Original post by Private Donut
I've tried putting arguments in the call to tellall(), but it always gives me an error: invalid operands of types `FILE*' and `const char[26]' to binary `operator<<'


The proposed solutions involve "putting arguments in the call", and should work. What *exactly* did you "try"?

Share this post


Link to post
Share on other sites
thanks everyone for the advice. I ended up getting it to work by passing by parameter, which was what I was trying to do before. The problem seemed to be that I wasn't passing the value as an fstream object.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this