I am still a newbe with C++ and wxWidgets.
I am trying to determine the best way to read a binary file into a wxString variable efficiently. Best for me means I prefer to read the entire record into a variable in one step (not character by character or line by line). My files will have ascii characters and some delimiters (could be crlf between lines or other delimiters such as ascii value 254). I would like to implement a wxWidgets only solution so my function can be used on different platforms.
I am using Windows 7 Pro (64bit), wxWidgets 3.1.2 and Code Blocks 17.12 with the integrated MinGW compiler (5.1.0).
I copied a bunch of functions from this forum and am trying to adapt to my purposes and can't figure out why method 1 doesn't work. The other two methods work but seem less efficient. It seems like I should be able to directly display and use the variable named ken in method 1 after I've read it.
Thoughts?
Thanks in advance.
Ken
Code: Select all
void mytest(){
{ //extra constructor forces automatic destructor call at end per below link
//above constructor idea from: https://forums.wxwidgets.org/viewtopic.php?t=4973
//my file is an ascii file with two lines, 17 characters long
//best wxstring conversion reference, so far: https://wiki.wxwidgets.org/Converting_everything_to_and_from_wxString
wxFileInputStream file(wxT("c:\\somefile.binary"));
wxTextInputStream text(file, wxT("\x09"), wxConvUTF8 ); //https://docs.wxwidgets.org/3.0/classwx_text_input_stream.html#a29eb32a28f132159ae8fc0e51a487678
//my question is: why does method one fail?
//method one fails, comment it out to run the next two methods (that both work)*
/*
int k;
k = file.GetLength();
file.SeekI(0,wxFromStart) ;//or wxFromEnd or wxFromCurrent
wxString ken,ken1;
file.Read(&ken,k); //read k bytes
//next convert k to wxstring (two methods shown) https://wiki.wxwidgets.org/Converting_everything_to_and_from_wxString
ken1 << k ; //ken1 = wxString::Format(wxT("%i"),k); //integer to string
wxMessageBox(ken1);//17
//function bombs on next line
wxMessageBox(ken);
//end of method one
*/
//method two works (very similar to method 1 but seems less efficient)
int k1;
k1 = file.GetLength();
file.SeekI(0,wxFromStart);//file.SeekI(k1, wxFromEnd);
char thing[k1];
file.Read(thing, k1);//was &thing corrected in forum dialog
wxString Strg1; //https://docs.wxwidgets.org/3.0/overview_unicode.html 8bitdata
Strg1 = wxString::FromUTF8(thing); //works
wxMessageBox("two: " + Strg1);
//method three works but loses the crlf characters when the results are displayed because readline is used
//https://wiki.wxwidgets.org/Reading_text_from_a_file
wxFileInputStream input(wxT("C:\\somefile.binary"));
//notice next line below says wxText... and not wxFile...
wxTextInputStream text2(input, wxT("\x09"), wxConvUTF8 );
wxString Strg2;
while(input.IsOk() && !input.Eof() )
{
wxString line=text2.ReadLine();
Strg2+=line ;
}
wxMessageBox("three: " + Strg2);
} // <- destructor called automatically per comment above
}