Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I delete the first line of a Text File??

1,187 views
Skip to first unread message

Jay Jackson

unread,
Apr 5, 1998, 4:00:00 AM4/5/98
to

Hello,

I want to delete the first line of a text file, but only temporarily. I
need to strip the contents of the text file after the first line, but leave
it in tact after I'm done with it. I'm not familiar with I/O routines, so
general answers won't be too helpful.

Thanks for any help you can give.

Peter Below

unread,
Apr 5, 1998, 4:00:00 AM4/5/98
to

> I want to delete the first line of a text file, but only temporarily. I
> need to strip the contents of the text file after the first line, but leave
> it in tact after I'm done with it. I'm not familiar with I/O routines, so
> general answers won't be too helpful.
>
Jay,

you cannot do this kind of operation on the file itself so one normally
copies to a second file. I'm not too clear from your wording what you want
to do with the file and the first line. To delete anything after the
first line one would do something like this:

Var
F: Textfile;
S: String;

Assignfile( F, filename );
Reset( F );
ReadLn( F, S );
Closefile( F );
Rewrite( F );
WriteLn( F, S );
Closefile (F);

If the file is not a multimegabyte monster you may consider loading it into a
TStringlist instance for processing.

Var
sl : TStringlist;
S : String;

sl:= TStringlist.Create;
try
sl.LoadFromFile( filename );
If sl.Count > 0 Then Begin
S := sl[0];
sl.Delete(0);
// sl now contains the rest of the lines, S the first line
.... process lines

End;
sl.SaveToFile( filename );
finally
sl.free;
end;

Peter Below (TeamB) 10011...@compuserve.com)


Jay Jackson

unread,
Apr 5, 1998, 4:00:00 AM4/5/98
to

Sorry for not being clear. The problem arise from the fact the BDE can't
import text files is the first line represent the field names. So first I
want to get the fields names out of the file (done) then import the data
(2nd through n lines). That's why I was asking how do I delete the first
line. If user gives me a file, I want to give it back to them in the same
state they gave it to me (1st line included).

If I'm reading your example correctly, the first part is simply coping the
file. The second part actually deletes the first line - correct? Is there
an alternative to the stringlist method (jut in case I get a huge DB)?

Thanks.

Peter Below <10011...@compuserve.com> wrote in message ...

Jay Jackson

unread,
Apr 5, 1998, 4:00:00 AM4/5/98
to

Peter,

I'm having a problem with the code (which probably means I don't really
understand what I'm doing). If 's' is just a string, how does the entire
text file 'f' get assigned to it? When I finish this routine, the text file
is empty. Also, I never defined 's'. My code is below.

Var
F: Textfile;
sl : TStringlist;
S : String;
begin
Try
FormCreateSchema := TFormCreateSchema.Create(Self);
// FormCreateSchema.Show;
FormCreateSchema.DefineFields('D:\delphi\importexport\jay.txt');
Finally
FormCreateSchema.Close;
End;

Assignfile( F, 'D:\delphi\importexport\jay.txt');


Reset( F );
ReadLn( F, S );
Closefile( F );
Rewrite( F );
WriteLn( F, S );
Closefile (F);


sl:= TStringlist.Create;
try
sl.LoadFromFile(s); // what did the proc above save the file as?


If sl.Count > 0 Then Begin
S := sl[0];
sl.Delete(0);
// sl now contains the rest of the lines, S the first line

// .... process lines

End;
sl.SaveToFile('D:\delphi\importexport\jay2.txt');
finally
sl.free;
end;
table1.open;
end;


Peter Below

unread,
Apr 6, 1998, 3:00:00 AM4/6/98
to

Jay,

are you perhaps using Delphi 2.0 (not 2.01)? There was a bug in the system
unit of that version that caused a Rewrite after a Closefile to not work
properly, one has to repeat the AssignFile as well. The code was supposed
to leave the file with only the first line in it.

To copy anything *but* the first line to another file one would have to do
this:

Procedure CopyAfterFirstLine(
Const sourcefile, targetfile: String );
Var
S: String; { Caveat! If lines are longer than 255 characters this will
NOT work with Delphi 1! }
source, target: Textfile;
Begin
If AnsiCompareText( sourcefile, targetfile ) = 0 Then
raise
Exception.Create(
'CopyAfterFirstLine: source and target cannot have the '+
'same filename!' );

AssignFile(source, sourcefile );
AssignFile(target, targetfile );
Reset(source);
try
ReWrite( target );
try
ReadLn( source, S );
While not Eof(source) Do Begin
ReadLn(source, S);
WriteLn( target, S );
End;
finally
Closefile( target );
end;
finally
Closefile( source );
end;
End; { CopyAfterFirstLine }

You would call this like

CopyAfterFirstLne('D:\delphi\importexport\jay.txt',
'D:\delphi\importexport\jay.tmp')

There is no way around creating a second file if you want to avoid loading
the file completely into memory first (e.g. into a stringlist). If for some
reason you absolutely need to have the file retain the filename you have to
rename it first and then specify the original name as targetname in the
procedure above.

Peter Below (TeamB) 10011...@compuserve.com)


0 new messages