Thread: wxFile problem
View Single Post
Old Aug 24th, 2007, 5:56 PM   #1
Lesliect6
Programmer
 
Join Date: Aug 2005
Posts: 68
Rep Power: 4 Lesliect6 is on a distinguished road
wxFile problem

Hello,

I have been trying to figure this out myself, but without success. I have recently started wxWidgets programming, and I was trying to find a way to write a string to an existing file. The name of the file is a string which I extract from a textBox. Now, in wxWidgets, the string type contained in a textBox is a wxString, so I cannot use it with the standard ofstream. There is a wxFile class, but the Write() method produces a weird error. Here is the code and the error message :

wxFile *file_to_write_to = new wxFile(WxEdit1->GetLineText(1));
if (!file_to_write_to->IsOpened()) // if error while opening file
    ErrorDialog->ShowModal();
else // if file could be opened
{
    file_to_write_to->Seek(pos); // go to a given position in the file
    wxString x="string to write";
    file_to_write_to->Write(x);
    delete file_to_write_to;
}

The program compiles correctly, but when I try to execute this code, I get :

can't write to file descriptor 3 (error 5: access refused)

Now I am pretty sure I have access to everything on my computer, I am in Administrator mode, the file I am opening is just an example file created using Notepad, "example.txt", there are no writing restrictions, no read-only, everything is fine. I get this error with every other file too. I narrowed down the problem and what I can be sure of is that the file IS opened successfully. The problem occurs after the "delete file_to_write_to;", even if I change it to "file_to_write_to->Close();". However, nothing is written to the file.

If there is an easier way to write to a file, please let me know, or help me out with this code, which appears to me to be logical.

Thank you,

Leslie
Lesliect6 is offline   Reply With Quote