Recent

Author Topic: Reading the last line of a text file.  (Read 15268 times)

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: Reading the last line of a text file.
« Reply #15 on: August 28, 2015, 11:38:10 pm »
It is an abstract concept to, at least, make ignatius3399 aware of the limitation presented in a solution and, hopefully, to stimulate another solution.  O:-)

No, you're just confusing the issue.
Given the description, it is unlikely that the lines in the tetx file are going to be > 4 MB long.
And if the file isn't massive (and why would you think so) a more simple approach will suffice, using the KISS principle.

Why don't we wait all until ignatius3399 gives us some answers?
That way we can help him with a solution tailored to his situation.

Bart

u2o

  • Jr. Member
  • **
  • Posts: 72
  • No message
Re: Reading the last line of a text file.
« Reply #16 on: August 29, 2015, 08:39:43 am »
I think the problem is UTF8 with BOM. If you save a text file with notepad (MS notepad), it will set the character encoding to UTF8 with BOM...

http://stackoverflow.com/questions/2223882/whats-different-between-utf-8-and-utf-8-without-bom

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: Reading the last line of a text file.
« Reply #17 on: August 29, 2015, 02:33:21 pm »
I think the problem is UTF8 with BOM. If you save a text file with notepad (MS notepad), it will set the character encoding to UTF8 with BOM...

Why do you think that?
I can see nothing in this thread that suggests this, nor would it be a problem for any of the given methods.

Bart

u2o

  • Jr. Member
  • **
  • Posts: 72
  • No message
Re: Reading the last line of a text file.
« Reply #18 on: August 29, 2015, 03:44:19 pm »
Why do you think that?
I can see nothing in this thread that suggests this, nor would it be a problem for any of the given methods.

Hi Bart!

It's something that don't understand those who have used 'ANSI' all his life, has never encountered such problems.

By default, Lazarus is not prepared to work with UTF8, functionality must be activated ... Even there are many functions to this day, fail with UTF8 strings.

If I use an operating system that uses an UTF8 code page, (spanish / russian, etc.) I will have many problems that have not experienced those using standard code page.

One example is: read txt files that have been created or edited with M$ Notepad, M$ Notepad saves it as UTF8 With boom without asking (it occurs on an system with non standard code page).
« Last Edit: August 29, 2015, 04:13:15 pm by u2o »

u2o

  • Jr. Member
  • **
  • Posts: 72
  • No message
Re: Reading the last line of a text file.
« Reply #19 on: August 29, 2015, 04:00:18 pm »
...nor would it be a problem for any of the given methods.

The resolution of the problems with utf8 often quite complex in some cases...

But all can funny testing quickly: Length vs UTF8Length on txt files. Please, test the attached file... Try line read, Lengt, etc...

We need know how it has saved, or create a method to detect the encoding, to avoid this problems...
« Last Edit: August 29, 2015, 04:11:32 pm by u2o »

u2o

  • Jr. Member
  • **
  • Posts: 72
  • No message
Re: Reading the last line of a text file.
« Reply #20 on: August 29, 2015, 04:20:48 pm »
One example is: read txt files that have been created or edited with M$ Notepad, M$ Notepad saves it as UTF8 With boom without asking (it occurs on an system with non standard code page).

Among these tests, I see that M$ Notepad on Windows 7 no longer saves by default files as UTF8 with Bom, but this was different a year ago ... Maybe an update has corrected this.
Equally the previously created files, maintain their own encoding. Eg, a file created on XP 3 years ago, which I open to edit and save it again. M$ Notepad will not saves it in ANSI, because if it do this, we will lose data.
« Last Edit: August 29, 2015, 04:27:57 pm by u2o »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Reading the last line of a text file.
« Reply #21 on: August 29, 2015, 04:28:49 pm »
hello,
taazz are you sure for your second procedure ? seems not to work : no result for me with a 64Mb File. 
I have a similar code  using a TfileStream :
Code: [Select]
procedure TForm1.LastLineClick(Sender: TObject);
var  M: TFileStream;
var  s: String;
var i: Integer;
begin
  M := TFileStream.Create('c:\tmp\test.txt', fmOpenRead);
  try
    // 2000 is the max size for a line. Change this value if greater.
    if M.Size > 2000 then
    i := 2000
    else
    i := M.Size;
    SetLength(s, i);
    M.Seek(-i, soFromEnd);
    M.Read(s[1], i);
    s := Trim(s);
   //find last line feed character (#10)...
    i := Length(s);
    while (i > 0) and (s[i] <> #10) do
     Dec(i);
     if i > 0 then
     Delete(s, 1, i);
  ShowMessage('Last Line : ' + s);
  finally
    M.Free;
  end;
end;           

Friendly J.P
« Last Edit: August 29, 2015, 04:31:21 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: Reading the last line of a text file.
« Reply #22 on: August 29, 2015, 04:34:02 pm »
No result? You mean the result is an empty string? Maybe your last line IS an empty string. In that case you need the second to last line :)

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Reading the last line of a text file.
« Reply #23 on: August 29, 2015, 04:40:02 pm »
yes rvk , it is an empty string but with my code i have the string of the real last line of the file  :P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Reading the last line of a text file.
« Reply #24 on: August 29, 2015, 08:24:05 pm »
hello,
taazz are you sure for your second procedure ? seems not to work : no result for me with a 64Mb File. 
I haven't compile or test the results so I'm not sure for any of them. They where posted to answer the question "how is done in pascal" not to present a complete and working solution. I do assume from TS posts that he already has enough experience to use the hints in my snippets to convert his knowledge from c to pascal if not I can answer every new question that pops even write a more generic function but I must be sure that he wants a ready to use function and not an easy to learn one.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: Reading the last line of a text file.
« Reply #25 on: August 30, 2015, 12:10:23 am »
By default, Lazarus is not prepared to work with UTF8, functionality must be activated ... Even there are many functions to this day, fail with UTF8 strings.

Excuse me?
The entire LCL (and therefore Lazarus itself) is UTF8 based.
For instance TMemo, TSynEdit support filenames in UTF8 encoding and can load files with UTF8 BOM.

If I use an operating system that uses an UTF8 code page, (spanish / russian, etc.) I will have many problems that have not experienced those using standard code page.

Most Linux distro's default to UTF8, and Lazarus has no problem whatsoever with that.

One example is: read txt files that have been created or edited with M$ Notepad, M$ Notepad saves it as UTF8 With boom without asking (it occurs on an system with non standard code page).

See my comment above.
TStringList will also load such a file without any problem.
(Tested on W7, with Lazarus trunk, fpc 2.6.4, and yes I verified the BOM is actually there.)

So, with regard to this particular problem, your comments make no sense,
Note that UTF8 (as far as Pascal is concerned) is just a special case of ansistring (it's just not single-byte).For this problem it also does not matter wether you use Length or Utf8Length, since we are searching for a LineEnding, which will always be a single-byte.

IMO the encoding of the file (ANSI vs UTF8) is not relevant to this particular problem.

Bart

ignatius3399

  • Newbie
  • Posts: 5
Re: Reading the last line of a text file.
« Reply #26 on: August 30, 2015, 02:54:46 am »
First of all, thank you all for the help. It works. But this brings up another issue. I have a const:

Const
        q1 : integer = 0;
        q2 : integer = 0;
        q3 : integer = 0;
        q4 : integer = 0;
        q5 : integer = 0;

The problem i'm having is, that it doesn't "remember" the q# integer values. When I log into the BBS, and vote, it increments the vote just fine and does "remember." Now, when I log off of the BBS, then login and vote again, the q# variables get "reset."
So, I need to implement some of for it to "remember" the current value.

Anyone know a way to solve this issue?

Again, thank you for all of your help.

-ignatius3399

ignatius3399

  • Newbie
  • Posts: 5
Re: Reading the last line of a text file.
« Reply #27 on: August 30, 2015, 03:34:35 am »
I forgot to include the incrementing code.

        inputl(s,2);

        If s = '1' then Inc (q1, 1);
        If s = '2' then Inc (q2, 1);
        If s = '3' then Inc (q3, 1);
        If s = '4' then Inc (q4, 1);
        If s = '5' then Inc (q5, 1);

"inputl" is a function in the Impulse source that reads the input, and allows for a number of characters in that string.
 And yes, I do realize this is a very simplistic question. I apologize for my ignorance. I'm new to (Free) Pascal.
« Last Edit: August 30, 2015, 03:39:00 am by ignatius3399 »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Reading the last line of a text file.
« Reply #28 on: August 30, 2015, 11:18:54 am »
You could try saving the values to a file, and re-loading them before you log on again?
Also, make sure you have specified
Code: [Select]
{$J+}
before the const declarations.
« Last Edit: August 30, 2015, 11:22:45 am by howardpc »

ignatius3399

  • Newbie
  • Posts: 5
Re: Reading the last line of a text file.
« Reply #29 on: August 30, 2015, 11:43:07 am »
My thoughts exactly. Problem is, I do not know how to do that. :(

 

TinyPortal © 2005-2018