Board index » delphi » Getting position of carriage return from file

Getting position of carriage return from file

Hello all,
I am trying to write a program that will open a file and then read the file
into a string variable.  I want to be able to search the variable to find what
posistion the carriage return (#13) is located at.  When I step thru the code
and get to the line: readln(filename,stringtemp) and look at the value of
stringtemp, I do not see the carriage return character #13. The end result of
the program is to find the amount of lines in the file so I could then seek to
an arbitary section of the file based on how many lines it contains.  Something
like tail in Linux.  I am probably going about this the wrong way, but live and
learn I guess.  If someone could help with this I would appreciate it very
much.

Thank you,
HowarD

 

Re:Getting position of carriage return from file


Quote
TorchIT (y...@somehost.somedomain) wrote:

: I am trying to write a program that will open a file and then read the file
: into a string variable.  
: I want to be able to search the variable to find what
: posistion the carriage return (#13) is located at.  
: When I step thru the code
: and get to the line: readln(filename,stringtemp) and look at the value of
: stringtemp, I do not see the carriage return character #13.

      If you use ReadLn, I don't think you will see
the #13 or the #10, that is what ReadLn does, read
up until the #13 carriage return and #10 line feed.
      I think you will have to use Read(txtfil); to
see the #13.
      And I think Unix and Usenet removes the #13
on text files I put in my homepage if I use a Unix
editor, so that can be a problem.

      I found a trick that replaces the #13, I load
the file into TP3.0 and do an immediate ^Q ^C to
the end of the file, then do up-arrow back up to
the beginning of the file, and the lines all
format correctly and when saved, the #13's are
there.

Ken Fischer

---

Re:Getting position of carriage return from file


Quote
TorchIT wrote in message

<8BF6D8AFE877EDEA.B2F5FA81B5B85CEA.A9DFBFFDBD4EF...@library-proxy.airnews.ne
t>...

Quote
>Hello all,
>I am trying to write a program that will open a file and then read the file
>into a string variable.  I want to be able to search the variable to find
what
>posistion the carriage return (#13) is located at.  When I step thru the
code
>and get to the line: readln(filename,stringtemp) and look at the value of
>stringtemp, I do not see the carriage return character #13. The end result
of
>the program is to find the amount of lines in the file so I could then seek
to
>an arbitary section of the file based on how many lines it contains.
Something
>like tail in Linux.  I am probably going about this the wrong way, but live
and
>learn I guess.  If someone could help with this I would appreciate it very
>much.

>Thank you,
>HowarD
> You are not giving enogh info. Please send program to:

Tysin...@northstate.net

Re:Getting position of carriage return from file


Quote
Ken Fischer wrote:
> [...]
>       If you use ReadLn, I don't think you will see
> the #13 or the #10, that is what ReadLn does, read
> up until the #13 carriage return and #10 line feed.
>       I think you will have to use Read(txtfil); to
> see the #13.

Yes, readln() will filter out CRLF combinations.  One side-effect though is
that it doesn't detect only CR (#13) or LF (#10).

Quote
>       And I think Unix and Usenet removes the #13
> on text files I put in my homepage if I use a Unix
> editor, so that can be a problem.

It depends mostly on the file system.  DOS uses #13#10 as a line terminator.
Unix uses just #10 as a line terminator.  If you use FTP to transfer files in
text (ASCII) mode, files will be transferred so that the line terminator is
translated to whatever's appropriate for the receiving end.

Quote
>       I found a trick that replaces the #13, I load
> the file into TP3.0 and do an immediate ^Q ^C to
> the end of the file, then do up-arrow back up to
> the beginning of the file, and the lines all
> format correctly and when saved, the #13's are
> there.

Another simple way to transfer a Unix-formatted file to DOS.  Load it in
MS-DOS's edit program and then resave it (<Alt> <F> <S>).

Also, I wrote a relatively smart converter which will automatically convert
DOS text files to Unix and vice versa.  Source is available in SWAG.

Quote
> Ken Fischer

> ---

--
Scott Earnest            | SPAM protection in effect. Remove  |
setech@_ix.netcom.com    | "_" as needed for true addresses.  |
earnests@_homenet.lm.com |    UIN:1136443  EFnet:pale_blue    |
sinykal@_{*word*104}space.org  | URL: http://www.netcom.com/~setech |

Re:Getting position of carriage return from file


Quote
Scott Earnest <setech@_ix.netcom.com> wrote:

>Yes, readln() will filter out CRLF combinations.  One side-effect though is
>that it doesn't detect only CR (#13) or LF (#10).

Not quite.  Readln always recognizes a CR (#13) as EOL.  It removes a
LF (#10) only when it immediately follows a CR (#13).  All other LF's
(#10's) will be included as part of the line.  For example, when
reading a text file that ends with a LF-CR (#10#13) combination,
readln for all versions of TP/BP since 1.0 recognizes the CR (#13) as
EOL but includes the LF (#10) as the last character of the line.

    ...red
--
Support the anti-Spam amendment
  Join at http://www.cauce.org/

Re:Getting position of carriage return from file


I think the best way is to open file not as predefined TEXT but file of byte;
Then use readblock(..) and search for any byte(s) you want (CR,LF, CR/LF pair)
depending of kind of file (unix,pc etc.)
Milic Tomislav

Quote
TorchIT wrote:
> Hello all,
> I am trying to write a program that will open a file and then read the file
> into a string variable.  I want to be able to search the variable to find what
> posistion the carriage return (#13) is located at.  When I step thru the code
> and get to the line: readln(filename,stringtemp) and look at the value of
> stringtemp, I do not see the carriage return character #13. The end result of
> the program is to find the amount of lines in the file so I could then seek to
> an arbitary section of the file based on how many lines it contains.  Something
> like tail in Linux.  I am probably going about this the wrong way, but live and
> learn I guess.  If someone could help with this I would appreciate it very
> much.

> Thank you,
> HowarD

Re:Getting position of carriage return from file


Milic Tomislav heeft geschreven in bericht <354D6010.5B0F1...@yahoo.com>...

Quote
>I think the best way is to open file not as predefined TEXT but file of
byte;
>Then use readblock(..) and search for any byte(s) you want (CR,LF, CR/LF
pair)
>depending of kind of file (unix,pc etc.)
>Milic Tomislav
>TorchIT wrote:

>> Hello all,
>> I am trying to write a program that will open a file and then read the
file
>> into a string variable.  I want to be able to search the variable to find
what
>> posistion the carriage return (#13) is located at.  When I step thru the
code
>> and get to the line: readln(filename,stringtemp) and look at the value of
>> stringtemp, I do not see the carriage return character #13. The end
result of
>> the program is to find the amount of lines in the file so I could then
seek to
>> an arbitary section of the file based on how many lines it contains.
Something
>> like tail in Linux.  I am probably going about this the wrong way, but
live and
>> learn I guess.  If someone could help with this I would appreciate it
very
>> much.

>> Thank you,
>> HowarD

When all lines are less than 256 chars you could use something like this
while nof eof(f) do
begin
    readln(f,s);
    inc nolines;
end;
writeln(nolines);

Other Threads