Read text file backwards 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.
Marcus Frenkel
Experienced Solver
Experienced Solver
Posts: 79
Joined: Thu Sep 25, 2008 12:14 am

Post by Marcus Frenkel » Mon Sep 29, 2008 2:02 pm

I'm on Win XP using MSVC 2008, file.clear is not removed. Here is the code I use:

Code: Select all

		struct SLineInfo
		{
		int iLength;
		int iOffset;
		};
		  const int bufferSize = 1024 * 9;
			char buffer [bufferSize];

			vector<SLineInfo> nLines;
			ifstream file ("C:\\test.txt");

			// find all the lines and their offset
			while (!file.eof ())
			{
				SLineInfo sli;
				sli.iOffset = file.tellg ();
				file.getline (buffer, bufferSize);
				sli.iLength = file.gcount ();
				nLines.push_back (sli);
			} //while

			// clear eof bit
			file.clear ();

			// print them backward
			for (vector<SLineInfo>::reverse_iterator it = nLines.rbegin (); it != nLines.rend (); ++it)
			{
				// clear eof bit
				file.clear ();
				file.seekg ((*it).iOffset, ios_base::beg);
				file.getline (buffer, bufferSize);

			
				wxString mystring(buffer, wxConvUTF8);
				wxMessageBox(mystring, wxT(""),wxYES_NO | wxCANCEL, frame);

			} //for

			file.close ();
It's strange that it works at your side and not on mine if you use the same code above.

Marcus Frenkel
Experienced Solver
Experienced Solver
Posts: 79
Joined: Thu Sep 25, 2008 12:14 am

Post by Marcus Frenkel » Mon Sep 29, 2008 2:35 pm

I'm not really familiar with debuging.

Can anybody please check the code on windows platform. The potential bug is not related to string to wxString conversion because I tried to use ofstream to save each line (from the 2nd loop) to another text file and the result is the same - empty file or some weird characters in case the test file has more than about 10 lines.

Thanks, Marcus

Romas
I live to help wx-kind
I live to help wx-kind
Posts: 176
Joined: Mon Jun 16, 2008 11:07 am
Location: Kaunas

Post by Romas » Mon Sep 29, 2008 2:45 pm

I've tested on Windows Vista + Visual Studio 2008 and it worked great (with file with 73 lines). :)

Marcus Frenkel
Experienced Solver
Experienced Solver
Posts: 79
Joined: Thu Sep 25, 2008 12:14 am

Post by Marcus Frenkel » Mon Sep 29, 2008 2:55 pm

Romas, thanks for the ongoing attention to this issue. Did you tried the code with the

Code: Select all

wxMessageBox(mystring, wxT(""),wxYES_NO | wxCANCEL, frame);
included? Did you get the text of each line in a message box?

Thanks, Marcus

Romas
I live to help wx-kind
I live to help wx-kind
Posts: 176
Joined: Mon Jun 16, 2008 11:07 am
Location: Kaunas

Post by Romas » Mon Sep 29, 2008 3:14 pm

Yes :), but I shorten the line to:
wxMessageBox(mystring, wxT(""));

But this is not the case. Why this doesn't work for you - I don't know. Try to read file forward (change reverse_iterator with iterator). Try to reopen file for actual reading (maybe the file.clear doesn't work as I want). Try to replace fstream with C fopen, fread, fseek... Try to play :)

Marcus Frenkel
Experienced Solver
Experienced Solver
Posts: 79
Joined: Thu Sep 25, 2008 12:14 am

Post by Marcus Frenkel » Mon Sep 29, 2008 3:29 pm

OK found it. It doesn't work when I create the test text file using windows notepad. When I create the file using wxWidgets it's working great. The issue was something related to line termination.

Thanks for all the help!

Marcus Frenkel
Experienced Solver
Experienced Solver
Posts: 79
Joined: Thu Sep 25, 2008 12:14 am

Post by Marcus Frenkel » Mon Sep 29, 2008 3:35 pm

Just one more thing to make my life calm and happy:

When I use wxFile to write into file, the file is not load into memory right? I know this 99% but I need to be sure. Or better use wxTextOutputStream?

Marcus

Romas
I live to help wx-kind
I live to help wx-kind
Posts: 176
Joined: Mon Jun 16, 2008 11:07 am
Location: Kaunas

Post by Romas » Mon Sep 29, 2008 3:51 pm

n.p. for the help :)
wxFile shouldn't load file content to memory, streamed classes should.

Post Reply