Writing to one file from different functions

Started by
7 comments, last by Private Donut 18 years, 1 month ago
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]
Advertisement
Hi, please wrap your code in [ source lang="cpp"][/source] or </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.
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].
If at first you don't succeed, redefine success.
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.
Write more poetry.http://www.Me-Zine.org
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;}
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
in the global, shouldn't functiontwo have no parameters?

Beginner in Game Development?  Read here. And read here.

 

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.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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"?
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.

This topic is closed to new replies.

Advertisement