Access deniedwhen trying to write wxString into file. Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
artorias807
In need of some credit
In need of some credit
Posts: 5
Joined: Wed May 06, 2020 6:47 am

Access deniedwhen trying to write wxString into file.

Post by artorias807 » Thu May 14, 2020 9:57 am

Hello!
I am trying to write some data into a .txt file. The idea is that the first time i want to write into the file the file doesnt exists so i want to create it myself. The file is created and can be opened as well so that part works fine. But when it reaches the part where i tell the program to write the wxString type data into the opened file it throws an error. The program itself keeps running it wont crash or anything but when i open the txt file in the folder it is located the file is empty.

This is the code responsible for the whole writing into file part:

Code: Select all

void cMain::WriteToFile(wxString s)
{

	wxFile file;
	if (!file.Exists("../Daily Report.txt")) {
		file.Create("../Daily Report.txt");
	}
	file.Open("../Daily Report.txt");
	if (file.IsOpened()) {
		file.Write(s);
	}
	file.Close();
}
And this is the error message i receive:
Attachments
access.PNG
access.PNG (3.51 KiB) Viewed 86 times

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2387
Joined: Sun Jan 03, 2010 5:45 pm

Re: Access deniedwhen trying to write wxString into file.

Post by PB » Thu May 14, 2020 10:42 am

You cannot write to a file you Open()ed in the default read mode, you probably want to use write_append or similar instead.

See wxFile documentation for more information
https://docs.wxwidgets.org/trunk/classwx_file.html

You may also want to consider whether wxFile is the best class for writing text data and whether you should write texts with an automatic character encoding...

artorias807
In need of some credit
In need of some credit
Posts: 5
Joined: Wed May 06, 2020 6:47 am

Re: Access deniedwhen trying to write wxString into file.

Post by artorias807 » Sun May 17, 2020 7:34 pm

You are right!
Thank you!

Post Reply