Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   wxFile problem (http://www.programmingforums.org/showthread.php?t=13846)

Lesliect6 Aug 24th, 2007 5:56 PM

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

DaWei Aug 24th, 2007 6:10 PM

You can get the C string with the .c_str () method. Just pass that as the name of the file when you call wxFile::Open. You WILL need to open the file before writing to it.

Lesliect6 Aug 25th, 2007 6:23 AM

Thank you DaWei, but the problem still persists. I did exactly as you said, and now I get the error :
:

can't write to file descriptor 8 (error 9: wrong file descriptor)

The modified code is as follows :
:

wxFile *file_to_write_to = new wxFile(WxEdit1->GetLineText(0).c_str());
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 does not show the ErrorDialog, which means the file is opened correctly. However, nothing is written to it.

Thank you,

Leslie

dr.p Aug 25th, 2007 6:43 AM

You need to use wxFile::write as the second parameter to your wxFile constructor call, since the file is opened in read mode by default.

e.g.
:

wxFile *file_to_write_to = new wxFile(WxEdit1->GetLineText(0).c_str(), wxFile::write);

Your error message probably changed, by the way, because you didn't explicitly identify your c_str as a const char *. It looks like it's being interpreted as an integer, so you're actually calling the secondary constructor, which requires an integer value that identifies the file descriptor.

Check out the wxFile class in the widget docs for more details. You can download the docs from the wxWidget web site in a number of formats to have handy.

DaWei Aug 25th, 2007 7:24 AM

Absolutely. If you only give a single value to the constructor, without casting, it will be interpreted as a file descriptor. If you add the second argument, open mode, the first argument will be interpreted as a const char * without the cast.

As dr.p says, a visit to the documentation is in order.

Lesliect6 Aug 25th, 2007 8:33 AM

Thank you very much! Now it works! I should have read the documentation more closely. This was a valuable lesson.

Leslie


All times are GMT -5. The time now is 4:48 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC