FileStream, InputStream and Bit ignore 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
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

FileStream, InputStream and Bit ignore

Post by Natulux »

Hey guys

I have quite a large text file, which I want to read into a wxTextCtrl. The catch is: This text file may change rapidly.
So I initially read in the whole file and append it to the textctrl:

Code: Select all

wxFile file;
file.Open(fn_logFile.GetFullPath());
file.ReadAll(&ms_fileContent);
ms_fileContent = ms_fileContent.ToUTF8();

mtc_log->AppendText(mLFC.ms_fileContent); 
file.Close();
This takes about 400ms for a text file with 200k lines, which is good.
Now I want to update the textCtrl periodically.

I stored the length of my file

Code: Select all

wxFileOffset	mfo_fileLength = file.Length()
Therefore I know how much bits to read from the end of the file.

Code: Select all

size_t sizeBufferDif = mfo_fileUpdateLength - file.Length();
if (sizeBufferDif <= 0)
{
	return;
}
Unfortuantely, I couldn't find a method to read only certain bits from the large file. In standard c(++) I could have used "ignore", something like

Code: Select all

ifstream a1;
a1.open("a.bin", ios::in|ios::binary)

char tmp[256]
a1.read(tmp,256)
a1.ignore(16);         //THIS
a1.read(data, 256-16);
Is there an equivalent to "ignore" to any of the wxStreams I could use?
Reading in the whole file bit by bit takes at least some minutes, which is too long, let alone beeing repeated every 500ms:

Code: Select all

wxFileInputStream fis_input(mLFC.mfn_fileFullPath.GetFullPath());
wxTextInputStream tis_text(fis_input, wxT("\x0a"), wxConvUTF8);
wxString sNewContent;

wxChar* buffer = new wxChar[1024];
do
{
	fis_input.Read(buffer, 1024 * sizeof(wxChar));
	sNewContent.Append(buffer, 1024);    //wxString& Append(const wchar_t *pwz, size_t nLen)
} 
while (fis_input.LastRead() > 0);
	
wxMessageBox(sNewContent);
Regards
Natu
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: FileStream, InputStream and Bit ignore

Post by doublemax »

Use the source, Luke!
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: FileStream, InputStream and Bit ignore

Post by Natulux »

doublemax wrote:wxInputStream::SeekI
http://docs.wxwidgets.org/trunk/classwx ... bcee03822d
Oh my, that must be the blindness of a desperate. I even flew over that function, not recognizing its effect. Thank you. ;-)
It now looks like this:

Code: Select all

wxFileInputStream fis_input(mBaseLogFileContent.mfn_fileFullPath.GetFullPath());
fis_input.SeekI(mBaseLogFileContent.mfo_fileLength, wxFromCurrent);
wxTextInputStream tis_text(fis_input, wxT("\x0a"), wxConvUTF8);

wxString sNewContent;

while (fis_input.IsOk() && !fis_input.Eof())
{
	sNewContent << tis_text.ReadLine() + "\n";	
}
sNewContent.Trim();
mtc_log->AppendText(sNewContent);
Have a great day.
Natu
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: FileStream, InputStream and Bit ignore

Post by Natulux »

I ran into some parameter errors crashing the app while using the stream ("_invalid_parameter" and "_invoke_watson(...)").
So the code now looks like this (for future programmers search)

Code: Select all

wxString sNewContent;
try
{
	wxFileInputStream fis_input(mBaseLogFileContent.mfn_logFile.GetFullPath());
	fis_input.SeekI(mBaseLogFileContent.mfo_fileLength, wxFromCurrent);
	wxTextInputStream tis_text(fis_input, wxT("\x0a"), wxConvUTF8);

	
	while (fis_input.IsOk() && !fis_input.Eof())
	{
		sNewContent << tis_text.ReadLine() + "\n";	
	}
	//sNewContent.Trim();
	sNewContent.RemoveLast(wxString("\n").length());
	mtc_log->AppendText(sNewContent);
	mBaseLogFileContent.AppendFileContent(sNewContent);
}
catch(const wxStreamError &e)
{
	wxGetApp().DoLog("BaseLog streaming Error!");
}
Best
Natu
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: FileStream, InputStream and Bit ignore

Post by PB »

Natulux wrote:

Code: Select all

wxString sNewContent;
try
{
	<snipped>
}
catch(const wxStreamError &e)
{
	wxGetApp().DoLog("BaseLog streaming Error!");
}
wxStreamError seems to be an enum. Do wxWidgets actually throw it when an error is encountered? I have always thought wxWidgets do not throw any exceptions at all but return an error code (such as wxStreamError) instead...
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: FileStream, InputStream and Bit ignore

Post by Natulux »

PB wrote:wxStreamError seems to be an enum. Do wxWidgets actually throw it when an error is encountered? I have always thought wxWidgets do not throw any exceptions at all but return an error code (such as wxStreamError) instead...
Hm, good point I guess. I could solve the error back then, so I didn't test this issue further. It might be, that the 'thrown error' does not contain the error properly, but still it doesn't crash the program which is is better than nothing.

Have a good new year.
Natu
Post Reply