simple question on textfile

Hi experts,

I have a too much simple question for you. When I use readln on a textfile, delphi read the line until #13.
But if the end line character is #10, how can I read lines one by one other than reading characters one by one to detect endline?

Alph...
AlphomegaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LischkeCommented:
Hi Alph,

using the old-fashioned way you won't have any other possibility to read lines. They need to be marked with #13.

I'd suggest that you look at the more comfortable solutions like TStringList (LoadFromFile or CommaText) and/or file streams.

Ciao, Mike
0
simonetCommented:
Are you talking about Unix-style line breaks (#10) ?

Alex
0
AttarSoftwareCommented:
The way I did this, is to write a simple program that searches for #10 on its own, and then converts it to #13#10, which Delphi can handle...

Tim.
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

craig_capelCommented:
Can i ask how you are reading in the text files is it like this?

var
t: textfile;
tmp: string;


system.asign(t,'textfilename.ext');
syste.reset(t);
repeat
  readln(t,tmp);

until eof(t);
system.close(t);

What method are you using?


Craig C.
0
AlphomegaAuthor Commented:
craig,

my code is

assignfile(FicVar, FicName);
reset(FicVar);
while not seekEOF(FicVar) do
 begin
  readln(FicVar, InString);
  { do anything with InString}
 end;
closefile(FicVar);

simonet
In fact, the source file (text) is created by coldfusion and posted by web mail server. So it's effectivly Unix-style line breaks (#10)

So if there's no solution, I'll code a migration procedure.

Thanks
0
LischkeCommented:
I'd still recommend that you rewrite your code so:

var
  List: TStringList;
  I: Integer;

begin
  List := TStringList.Create;
  try
    List.LoadFromFile(FicName);
    for I := 0 to List.Count - 1 do
      // do something with List[I]...
  finally
    List.Free;
  end;
end;

Ciao, Mike
0
ptmcompCommented:
I agree with Lischke.
If you don't want to load all the text in memory use Streams (assign and reset caused many errors in my projects). You can look at the VCLs source of TStringList how to parse this CR/LF combiantions.
0
AlphomegaAuthor Commented:
Lischke

Sure, it's evident. it's the best solution. Thanks a lot!

Alph..
0
LischkeCommented:
So let me answer this question and ask if you need further help (in particular streams are really cool things :-)!

Ciao, Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simonetCommented:
>Sure, it's evident. it's the best
>solution. Thanks a lot!

No, it is not!

Sorry to disappoint you folks, but there is a much better, faster and easier way to do that for an entire file:

var
 ls : TStringList;
begin
 ls := TStringList.create;
 with is do
    try
       LoadFromFile('whatever.txt');
       text := AdjustLineBreaks(text);
       SaveToFile('whatever.txt');
    finally
       free;
    end;
end;


That's it. The AdjustLineBreaks function does all the dirty work of converting Unix-style line breaks to DOS (& Windows)-style line breaks. No parsing code is necessary and it is extremely fast.

The code above does all the work and, except for the filename, you don't have to change anything on it.

yours,

Alex
0
LischkeCommented:
Alex,

it seems you got the question wrong. It was not about converting Unix style line breaks to PC like line breaks, but to load a text file with whatever style it is and split it into single lines.

BTW: AdjustLineBreaks in your sample is totally superfluous as the LoadFromFile call will create single strings (lines) from the input file and SaveToFile will save them in usual PC style.

Ciao, Mike
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.