Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 24th, 2007, 5:56 PM   #1
Lesliect6
Programmer
 
Join Date: Aug 2005
Posts: 68
Rep Power: 3 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
Old Aug 24th, 2007, 6:10 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Aug 25th, 2007, 6:23 AM   #3
Lesliect6
Programmer
 
Join Date: Aug 2005
Posts: 68
Rep Power: 3 Lesliect6 is on a distinguished road
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
Lesliect6 is offline   Reply With Quote
Old Aug 25th, 2007, 6:43 AM   #4
dr.p
Programmer
 
dr.p's Avatar
 
Join Date: Feb 2006
Location: Ohio
Posts: 93
Rep Power: 3 dr.p is on a distinguished road
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
dr.p is offline   Reply With Quote
Old Aug 25th, 2007, 7:24 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Aug 25th, 2007, 8:33 AM   #6
Lesliect6
Programmer
 
Join Date: Aug 2005
Posts: 68
Rep Power: 3 Lesliect6 is on a distinguished road
Thank you very much! Now it works! I should have read the documentation more closely. This was a valuable lesson.

Leslie
Lesliect6 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:22 PM.

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