Hi, as is about to become entirely apparent, I'm a newbie programmer and I'm having some problems with what should be simple operations.
I'm having some trouble writing to file and no matter what method I use I can't seem to get it to work as I hope it to. I use the code below to open a file and append some text to it, but when I open the file afterwards to check that it has worked, the file is no different to how it was before I attempted to write to it.
Even if I specify ios:trunc (whic should clear the whole file) it seems that there is no change made at all.
The code below contains three different methods of writing to file for means of testing (in the hope that even one of them works).
fstream OutputFile;
OutputFile.open("Notes.txt", ios::out|ios::app);
if (!OutputFile.good())
{
MessageBox(hwndContainer, "There has been a problem opening the file", "File error", MB_OK);
}
{
OutputFile << "Just write to file!" << endl;
OutputFile.write("Hello?",7);
OutputFile.put('Test');
}
OutputFile.close();
The file 'Notes.txt' is not open and is not ever opened anywhere else in the program, so it's not that I'm trying to open a file which is already open.
If I place a breakpoint on any one of the output statements, I can see that the program does run them, so I'm not sure what to think.
I've read several tutorials and threads on the use of fstream and I can't see any difference between the way that I'm using it and the way that I'm supposed to be using it. I've even used it for input earlier on in the program (from a different file which I then close). I'm completely stumped.
No doubt I'm making some silly fundamental error somewhere, but I just can't seem to find what it is that's causing the problem.
Any help would be appreciated, thanks.
Edit:
I moved the code to a different section of my program (to the end of WinMain) and it worked.
I guess then it has something to do with when I'm calling the code. Here's some more code from the place that I want it to be:
case IDC_ADDSYMBOL:
{
bool ReturnValue;
ReturnValue = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_NEWSYMBOL), hwndContainer, NewSymbolProc);
if ( ReturnValue )
{
//file stream - ios:app Input/output stream for appending.
fstream OutputFile;
OutputFile.open("Notes.txt", ios::out|ios::app);
if (!OutputFile.good())
{
MessageBox(hwndContainer, "There has been a problem opening the file", "File error", MB_OK);
}
{
OutputFile << "Just bloody write to file!" << endl;
OutputFile.write("Hello?",7);
OutputFile.put('Test');
}
OutputFile.close();
}
else
{
}
break;
}
This is in a switch checking the loword of wParam after receiving a WM_COMMAND notification. IDD_NEWSYMBOL is an input form. ReturnValue is true if the user clicked OK and the input form was filled in correctly, otherwise it's false.