Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading from text files

Status
Not open for further replies.

JustinWillis

IS-IT--Management
Feb 6, 2003
21
GB
Hi, I am currently trying to read from a text file using ReadLn(TF, S); but if there is a blank line in the text file then ReadLn seems to automatically move onto the next line that has something on it instead of reporting S as being empty (length of 0), can some one help? I need to know when there is a blank line in the text file so it does not get missed out when writing it back to another text file.

Thanks in advance, sorry to ask such newbie questions.
Justin Willis.
 
This shows blank lines in StringGrid. What are you doing differently?
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  InFile: textfile;
  R: integer;
  S: string;
begin
  AssignFile(InFil,'d:\test.txt');
  Reset(InFil);
  R := 0;
  while not EOF(InFil) do
  begin
    Readln(InFil,S);
    StringGrid.Cells[0,R] := S;
    Inc(R);
  end;
end;
 
Hi, thanks for that, i think the only difference is i am adding the string to a TRichedit instead of a TStringgrid, also am using a repeat until loop instead of while loop, I must have mest up somewhere else in a strange way, will have to do some more debugging, if it still won't play ball then i'll post the code.

cheers,
Justin Willis.
 
ok, I found the problem, there was a third difference in my code, I was using
Code:
SeekEof
instead of
Code:
Eof
to test for end of file, for some reason it worked but removed all the blank lines. Found SeekEof in Help under text file routines, now I know! thanks so much.

Justin Willis.
happy.gif
 
Sorry to bother you all again but I am curious to know if any one can find a faster/more efficient way of running this very short piece of code...

Reads from source Textfile and Writes to dest Textfile. Don't want to do a raw file copy as only part of the text file needs to be copied.

Code:
AssignFile(SourceTF,'Source.txt');
Reset(SourceTF);
AssignFile(DestTF,'Dest.txt');
Rewrite(DestTF);
S := '';

Repeat
 ReadLn(SourceTF,S);
 WriteLn(DestTF,S);
Until Eof(SourceTF);

CloseFile(SourceTF);
CloseFile(DestTF);

Problem is that i don't need the data to go into S so it would be much better if I could do something like
Code:
WriteLn(DestTF,ReadLn(SourceTF));

So that the data goes straight from one file to the other, takes about 80 seconds to copy 13MB of text on my machine which doesn't impress me v much.

Thanks for any help!
JW
 
Can you not just use a file copying function e.g.
Code:
CopyFileTo('Source.txt', 'Dest.txt');
Clive [infinity]
 
unfortunatly I cannot as only part of the file needs to be copied (not the beggining), content always changes so as does the position to start copying from, I do not need S though as the copy can run right to the end of the source file. Hope that makes more sense.

Regards,
JW
 
How big are your files? 10MB only takes about 2 seconds including program startup. The only thing I can think that might help is to declare S as a fixed length string which may reduce garbage collection, but you would need to know the size of the longest line (including line termination).
 
Couldn't you just load the file in Tmemo and manipulate it from there than SaveToFile?

TMemo.Lines.LoadfromFile('C:\test.txt');
//manipulate TMemo...
TMemo.Lines.SaveToFile('C:\test.txt');

If you dont want the Tmemo to be visible you could set it to false or you could create a Tmemo object.
If you create a object.. remmember to free it.

Michael
 
It takes way too long to LoadFromFile into TStringlist and then SaveToFile (this is how i originally started until I started growing grey hairs waiting for it to finish ;) also the files may grow larger than 10MB in some instances so I need it to be as fast as poss.

Maybe it's just the s**t hardware i'm using but I can't get anywhere near 10MB in ~2 secs, unfortunatly the size of the line could be anything so can't limit the string.

I think i'll have to take a v close look at my loop to see if anything is slowing it down, the other things are
Code:
s := '';
and
Code:
if Eof(SourceTF) then abortloop := true;
etc.

Thanks for your help,

JW.
 
Copy File by stream is pretty fast...

procedure TMainForm.CopyFileStream(Source: string; Copy: string);
var
NewFile: TFileStream;
OldFile: TFileStream;
begin
OldFile := TFileStream.Create(Source, fmOpenRead or fmShareDenyNone);
try
NewFile := TFileStream.Create(Copy, fmCreate or fmShareDenyNone);

try
NewFile.CopyFrom(OldFile, OldFile.Size);
finally
FreeAndNil(NewFile);
end;
finally
FreeAndNil(OldFile);
end;
end;

michael
 
OH I'M SO STUPID! got so involved in how to make the code faster that I mist something very important, I am dealing with text files currently on a network mapped drive which would explain the slower speed huh? oh i'm so dum sometimes ;-)

The app will eventually work locally so should eliminate the speed issue, thanks v much for your help though guys, I will give the Filestream idea a go later to compare with current method.

Thanks again!
Code:
if JW>=Stupid then terminate;
:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top