Solved

Reading UNIX format (LF delimited) text files

Posted on 2002-04-29
5
276 Views
Last Modified: 2010-04-04
In all my Delphi applications dealing with text files so far, I've continued to use the "old" way of accessing files (Open as text, ReadLn, etc.).  The explanation in the manual of the "new" method seemed a little confusing to me, and I guess I was just too lazy to figure it out since I didn't see a benefit to using it.

'Nuff rambling, on to the problem / question:   If ReadLn is used on a LF-delimited file (rather than the DOS CRLF convention) it slurps the entire file in as one "line".  

Can someone provide me a simple & efficient program sample that will open a file, read a LF-delimited line at a time into a string variable, until EOF is reached?   Thanks.
0
Comment
Question by:charles_ebs
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6978689
I'm not sure but I thought TStringList.LoadFromStream (and LoadFromFile) could read UNIX text files correctly with  recognizing the line endings.
0
 
LVL 5

Accepted Solution

by:
alanwhincup earned 100 total points
ID: 6978703
Just did a search and found this unit which looks like it should be able to do it:

http://www.gnomehome.demon.nl/uddf/pages/conv.htm#conv5

Am still sure TStringList.LoadFromStream/LoadFromFile can handle these though....Can't test right now because have no Delphi on this system.

Cheers,

Alan
0
 
LVL 6

Expert Comment

by:zebada
ID: 6978964
Alan is correct, the LoadFromFile will read $0A delimited strings correctly. For small files probably the easiest way.

Here some (inefficient) code to do what you want.

procedure TForm1.ReadUnixLn(var fd: TextFile; var s: string);
var
  i: integer;
  c: char;
begin
  i := 0;
  while ( not Eof(fd) ) do
  begin
    Read(fd,c);
    if ( c=Chr(10) ) then
      exit;
    SetLength(s,i+1);
    s[i] := c;
    Inc(i);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  fd: TextFile;
  s: string;
begin
  try
    AssignFile(fd,'data.txt');
    FileMode := 0;
    Reset(fd);
    while ( not Eof(fd) ) do
    begin
      ReadUnixLn(fd,s);
      ShowMessage(s);
    end;
  except
    on e:EInOutError do
      ShowMessage(e.Message);
  end;
end;

0
 

Author Comment

by:charles_ebs
ID: 6979368
Oh yeah, forgot to mention that it does need to be able to process huge files, eliminating the solutions that read the whole thing into memory.

I'll take a closer look tomorrow at Alan's UDDF reference and see if I can use it. Thanks for the input so far.
0
 

Author Comment

by:charles_ebs
ID: 6984677
Nice unit; just add it to your program, and call AssignStreamFile instead of AssignFile, and it will read LF-delimited files.  

Only problem is, it doesn't handle CRLF files, and although I didn't make that clear, it needs to be able to handle reading either type.   SO..., here's the revised code section :

//F.Buffer [F.BufEnd] := Data.Buffer^ [Data.BufferOffset] ;
//Inc (Data.BufferOffset) ;
//Inc (F.BufEnd) ;
if Data.Buffer [Data.BufferOffset] <> #13 then
begin
  F.Buffer [F.BufEnd] := Data.Buffer^ [Data.BufferOffset] ;
  Inc (F.BufEnd) ;
end;
Inc (Data.BufferOffset) ;

I also removed the unused "Status" stuff to prevent compiler warnings.  Not necessary, just my preference.

Good enough for me!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi IDE crash without error message ... 7 99
Delphi 2 69
Tvertscrollbox like a whatsapp layout 5 36
How to make Sign in, using Clientdataset? 1 28
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question