For some reason i cant output to a file unless it was already created.
Here's my code.

fstream myfile;
    myfile.open (filename.c_str(), ios_base::in);
    if (!myfile)
    {
        myfile.close();
        myfile.open (filename.c_str(), ios::out);

        avatar.name = name;
        avatar.init();
        myfile << "Name = " << avatar.name << endl;
        myfile << "hp = " << avatar.hp << endl;
        myfile << "mana = " << avatar.mana << endl;
        myfile << "level = " << avatar.level << endl;
        myfile << "strength = " << avatar.strength << endl;
        myfile << "intelligence = " << avatar.intelligence << endl;
        myfile << "wisdom = " << avatar.wisdom << endl;
        myfile << "dexterity = " << avatar.dexterity << endl;
        myfile << "charisma = " << avatar.charisma << endl;
        myfile.close();
    }
    else
    {
        cout << name << " already exists" << endl;
    }

Now i create the file and output the data into it, but it wont work unless i have the file already created and open it.

Recommended Answers

All 3 Replies

You can create a file and write to it like this:

#include<iostream>
#include<fstream> // for file reading & writing
#include<conio.h> //for getch()

using namespace std;
int main(){
fstream myfile; //you can use ofstream for writing to a file and ifstream for reading from a file...& probably should
    myfile.open ("MyNewFile.txt"); //put whatever name you want
    if (!myfile) //um, why do you try to write to a file that has not opened?
    {
     //I would show an error if the file doesn't open
     cout<<"File not found.";
//        myfile.close();  how can you close it if you don't have one?
//        myfile.open (filename.c_str(), ios::out);
//
//        avatar.name = name;
//        avatar.init();
//        myfile << "Name = " << avatar.name << endl;
//        myfile << "hp = " << avatar.hp << endl;
//        myfile << "mana = " << avatar.mana << endl;
//        myfile << "level = " << avatar.level << endl;
//        myfile << "strength = " << avatar.strength << endl;
//        myfile << "intelligence = " << avatar.intelligence << endl;
//        myfile << "wisdom = " << avatar.wisdom << endl;
//        myfile << "dexterity = " << avatar.dexterity << endl;
//        myfile << "charisma = " << avatar.charisma << endl;
//        myfile.close();
    }
    else
    {
       // cout << name << " already exists" << endl;
       myfile<<"write something to the file.";
       cout<<"Writing to file.";
       myfile.close();
    }
getch();
return 0;
}

You can also create and open your file this way, which is more like what you're doing:

ofstream myfile;

    string filename = "MyNewFile2.txt"; //or whatever name you want
    myfile.open (filename.c_str(), ios::out);

mybad forgot to say, that was checking to see if the file already existed. So if it didnt open because it didnt exist then it would create and open one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.