int Finish()//outputs stored values to file
{
    ofstream walletfile;
    walletfile.open("C:/accounts/wallet.acnt", ios::trunc);
    walletfile << wallet;
    walletfile.close();
    ofstream accountfile;
    accountfile.open("C:/accounts/account.acnt", ios::trunc);
    accountfile << account;
    accountfile.close();
    ofstream savingsfile;
    savingsfile.open("C:/accounts/savings.acnt", ios::trunc);
    savingsfile << savings;
    savingsfile.close();
    return 0;
   }

I am a brand new programmer, just learning from the web, i wrote this function as part of an accounting program to write stored values to an output file, the program compiles, and runs without any problems, but when i activate this function from within the program nothing happens, when i retrieve the values from the file, they are exactly the same as the last time i drew from the file. HELP!

We need to know what types wallet, account, and savings are. Also you never check if the file is open, so perhaps it isn't opening properly? Also it would be better to make just one file and re-use it then to make 3 files like this:

ofstream file;
file.open("file1");
file<<data;
file.close();
file.open("file2");
file<<data;
file.close();
//...

We need to know what types wallet, account, and savings are.

No we don't. What types they are are irrelevant to the problem.

Add couts to follow the program flow to make sure you in fact are entering the function. And as Labdabeta suggests test (and display) the status of each open and write.

Edited 4 Years Ago by WaltP

Whenever you open a file to read or write, CHECK THAT IT OPENED! It's really easy to enter the wrong directory path or something. Do something like this:

std::ifstream inFile( "test.txt" );
if ( ! inFile.is_open() )
{
    std::cerr << "Error! Failed to open file" << std::endl;
    return 1;
}

Well, ur not the only newbie who lands up in this problem... Its pretty normal.. The solutions pretty simple..
Ur just not opening the directory properly.. Problem lies inside the double quotes.
1) Ur using the wrong slash, instead of backward slash ( \ ) you're using the forward slash ( / )
2) When using the slash, always use consecutive double slashes.. Because the compiler understands only 1 slash as a sign to acknowledge a special function.

For example :
When u press "\a", an audible bell is sounded..
If u press "\ ", a space is given by the compiler..
When u finally press " \\ ", the compiler understands it as \ ....

So ur code while opening the file should state :

walletfile.open("C:\\accounts\\wallet.acnt", ios::trunc);

Thats it, problem solved (remember to make the corrections to the rest) :)

Edited 4 Years Ago by adityatandon

Oh yeah completly forgot about the check, and adityatandon, that raises another question, when i use the backslash "\", my compiler returns errors, (i cant remember them off the top of my head), but when i use a forwardslash "/" it compiles and all my previous tests with file input output work properly. i guess thats not really a question, but a comment for you to debunk. thanks guys

P.S. its a warning not an error, "unknown escape sequence "(following letter)""

int Finish()//outputs stored values to file
{
    ofstream walletfile;
    walletfile.open("C:\accounts\wallet.acnt", ios::trunc);
    if ( walletfile.is_open())
    {
    walletfile << wallet;
    }
    walletfile.close();

    ...


    return 0;
}

I edited my script to this, still the same problem, did i do it wrong?

walletfile.open("C:\accounts\wallet.acnt", ios::trunc);

\a is CTRL-A
\w is CTRL-W

In C++ if you want a \ in a string you need to double it:
walletfile.open("C:\\accounts\\wallet.acnt", ios::trunc);

Like i said earlier, again ur mistake lies in the fact that you are using only a single slash, like i said above and like waltp just repeated, u have to use a double slash..

So ur code while opening the file should state :

walletfile.open("C:\\accounts\\wallet.acnt", ios::trunc);
Thats it, problem solved (remember to make the corrections to the rest) :)

Refer to my earlier post again.. See the double slash in the code closely...

For what it's worth, / vs \\ is a non-problem. Generally in C/C++ the languages treat both the same in file paths.

WaltP isn't that OS-dependant? Anyways, Windows (at least W7) supporst slashes too.

Another thing is, AFAIK if you supply the flags, you HAVE to specify the write flag too, so:

walletfile.open("C:\accounts\wallet.acnt", ios::trunc | ios::out);

should help too

I did the double slash and its still a problem, i might just go through and rewrite the program t see if i can smooth things out, however jaskij, correct me if im wrong, when ofstream is declaired the file is assumed to be output, and when ifstream is declaired its assumed input, no? and thank you WaltP for clairifying what was going on with those warnings

This article has been dead for over six months. Start a new discussion instead.