Board index » delphi » Strange problem in textfile

Strange problem in textfile

Hi,

I have encounter a strange problem when reading a text file.

var
  f1:TextFile;
 aline:string;
begin
  Assignfile(f1,'foo.dat');
  reset(f1);
  while not eof(f1) do
  begin
    readln(f1,aline);
   { do something about the text}
  end;
end;

When the computer hang up, I trace the program and found out that the
first line it read the aline actually contains the entire file. I
though may be the file does not containing any carriage return
character. But IDE show the the text enclosed in quotes for each line
followed by #$ after each line. All this in aline string.  However it
read into the memo component just fine. After I delete the first line,
then the program read each line corectly. My question is what's wrong
with the first line that cause readln to read the entire file into a
string. Why after I delete the first line, everything works.

Your help/ comment will be appreciated. Thanks.

chaoc

 

Re:Strange problem in textfile


Hi ChaoC.

When you look at the string and the characters which are between each line,
are there both the #$D and #$A?  If there is only one or the other, that may
be the problem.  The TextFile may only be able to distinguish and end of
line if both the CR and LF are present, where as a TStringList (as used in
the memo) perhaps can tell only with one of those characters.

HTH,

Cameron
_______________________
cameronm...@hotmail.com

Quote
ChaoC wrote in message <36ce10c5.2354...@news.flash.net>...
>Hi,

>I have encounter a strange problem when reading a text file.

>var
>  f1:TextFile;
> aline:string;
>begin
>  Assignfile(f1,'foo.dat');
>  reset(f1);
>  while not eof(f1) do
>  begin
>    readln(f1,aline);
>   { do something about the text}
>  end;
>end;

>When the computer hang up, I trace the program and found out that the
>first line it read the aline actually contains the entire file. I
>though may be the file does not containing any carriage return
>character. But IDE show the the text enclosed in quotes for each line
>followed by #$ after each line. All this in aline string.  However it
>read into the memo component just fine. After I delete the first line,
>then the program read each line corectly. My question is what's wrong
>with the first line that cause readln to read the entire file into a
>string. Why after I delete the first line, everything works.

>Your help/ comment will be appreciated. Thanks.

>chaoc

Re:Strange problem in textfile


On Sat, 20 Feb 1999 22:28:30 +1100, "Cameron McCormack"

Quote
<NOSPAMcameronm...@hotmail.com> wrote:
>Hi ChaoC.

>When you look at the string and the characters which are between each line,
>are there both the #$D and #$A?  If there is only one or the other, that may
>be the problem.  The TextFile may only be able to distinguish and end of
>line if both the CR and LF are present, where as a TStringList (as used in
>the memo) perhaps can tell only with one of those characters.

>HTH,

>Cameron

Hi Cameron.

Thank you for your response. However,  that does not explain the fact
that after I delete the first line using text editor (NoteTab) and
save it then everything works fine. Since I don't have the file on
hand (left it in office), I can not check whether the line is
deliminated with CR/LF.  

Re:Strange problem in textfile


Quote
ChaoC <ch...@flash.net> wrote:

: I have encounter a strange problem when reading a text file.
:
: var
:   f1:TextFile;
:  aline:string;
: begin
:   Assignfile(f1,'foo.dat');
:   reset(f1);
:   while not eof(f1) do
:   begin
:     readln(f1,aline);
:    { do something about the text}
:   end;
: end;

Hi chaoc,

This is probably not related, but I thought I should tell you that you
might want to put a CloseFile at the end of your routine.

- Mike

--
+------------------------+----------------------------------------+
| Michael J. Sussman     |    Looking for a fun game to play?     |
| Green Diamond Software |      Try DynaMine for free today!      |
| g...@primenet.com       | Check out http://www.primenet.com/~gds |
+------------------------+----------------------------------------+

Re:Strange problem in textfile


In article: <7ancuu$7f...@news.alphalink.com.au>  "Cameron McCormack"

Quote
<NOSPAMcameronm...@hotmail.com> writes:

> Hi ChaoC.

> When you look at the string and the characters which are between each line,
> are there both the #$D and #$A?  If there is only one or the other, that may
> be the problem.  The TextFile may only be able to distinguish and end of
> line if both the CR and LF are present, where as a TStringList (as used in
> the memo) perhaps can tell only with one of those characters.

Ie with Unix encoded text files. This is almost definately the problem. You'll have to use
blockread and blockwrite or else it may be possible that delphi's file device driver feature
can be taken advantage of.

--

Greg Lorriman co. inc. & sons Ltd

Re:Strange problem in textfile


Quote
In article <36ce10c5.2354...@news.flash.net>, ch...@flash.net (ChaoC) writes:
> But IDE show the the text enclosed in quotes for each line
>followed by #$ after each line.

What does the first line contain when you look at it in DOS / Debug ?

Alan Lloyd
alangll...@oal.com

Re:Strange problem in textfile


NoteTabe may be able to detect that only one character will delimit the
line, but then when saving it, it puts both CR and LF.

You could always load it into a TStringList and then save it again with
SaveToFile to make sure there is a CR/LF pair.

Cameron
_______________________
cameronm...@hotmail.com

Quote
ChaoC wrote in message <36cecf2e.1474...@news.flash.net>...
>On Sat, 20 Feb 1999 22:28:30 +1100, "Cameron McCormack"
><NOSPAMcameronm...@hotmail.com> wrote:

>>Hi ChaoC.

>>When you look at the string and the characters which are between each
line,
>>are there both the #$D and #$A?  If there is only one or the other, that
may
>>be the problem.  The TextFile may only be able to distinguish and end of
>>line if both the CR and LF are present, where as a TStringList (as used in
>>the memo) perhaps can tell only with one of those characters.

>>HTH,

>>Cameron

>Hi Cameron.

>Thank you for your response. However,  that does not explain the fact
>that after I delete the first line using text editor (NoteTab) and
>save it then everything works fine. Since I don't have the file on
>hand (left it in office), I can not check whether the line is
>deliminated with CR/LF.

Re:Strange problem in textfile


On Sun, 21 Feb 1999 12:38:30 +1100, "Cameron McCormack"

Quote
<NOSPAMcameronm...@hotmail.com> wrote:
>NoteTabe may be able to detect that only one character will delimit the
>line, but then when saving it, it puts both CR and LF.

>You could always load it into a TStringList and then save it again with
>SaveToFile to make sure there is a CR/LF pair.

>Cameron
>_______________________
>cameronm...@hotmail.com

Hi Cameron,

I think you are right. Thank you for your explaination. Although I can
not confirm the CR/LF delimination right now, I do known that the file
come from someone have access to  Unix machine. As Greg Lorriman
pointed out it may be Unix encoded text file.

I will be able to confirm this on Monday and will report back. Thank
you all.

Best regard.

ChacC

Re:Strange problem in textfile


Hi,

I have checked the text file and it is deliminated by $0A (LF) only.
The readln procedure requires the lines to be ended by $0D (CR) or
CR/LF combination. However, the TstringList of TMemo or TRichEdit use
Tstream to read the file and it only requires one of CR/LF as a line
separator.
The SaveToFile method of the TstringList automatically put CR+LF at
each line.

Thank you again for your response.

ChaoC

Other Threads