![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Aug 2005
Posts: 68
Rep Power: 3
![]() |
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Aug 2005
Posts: 68
Rep Power: 3
![]() |
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 |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Feb 2006
Location: Ohio
Posts: 93
Rep Power: 3
![]() |
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.
__________________
Neeley.org |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Aug 2005
Posts: 68
Rep Power: 3
![]() |
Thank you very much! Now it works! I should have read the documentation more closely. This was a valuable lesson.
Leslie |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| <td> height problem | grimpirate | HTML / XHTML / CSS | 19 | May 4th, 2007 6:01 AM |
| Changing icons problem | Pedja | C# | 8 | Mar 25th, 2006 8:03 AM |
| cgi/perl script + IE problem | joyceshee | Perl | 2 | Jan 24th, 2006 11:10 AM |
| help with recursion problem | the_new_guy_in_town | Java | 3 | Apr 7th, 2005 3:04 AM |
| string problem when passing in linked list | quantz | C++ | 0 | Feb 27th, 2005 10:11 AM |