Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

first line of file

ignazignaz Posts: 4Member
Any ideas how could I delete a first line of text file in Pascal?

Comments

  • zibadianzibadian Posts: 6,349Member
    : Any ideas how could I delete a first line of text file in Pascal?
    :
    Copy the file to a temporary file, except for the first line. Then rename the temporary file back to the original filename. This will give the appearance of the deletion of the line.
  • DrMartenDrMarten Posts: 748Member
    : : Any ideas how could I delete a first line of text file in Pascal?
    : :
    : Copy the file to a temporary file, except for the first line. Then rename the temporary file back to the original filename. This will give the appearance of the deletion of the line.
    :

    Even easier would be to open the file as input.

    Then using something like:->

    Repeat
    Read Inputstring$
    Until Inputstring$ = EOL

    Sorry but I can not 100% remember PASCAL syntax but I hope you get the idea. I can not remember if PASCAL reads a full line at a time though on input. Been over 10 years ago that I did any PASCAL.

    Skip the 1st line of the file this way then just read and write the rest of the file to another OUTPUT file.

  • GaashiusGaashius Posts: 349Member
    You have written:
    > Any ideas how could I delete a first line of text file in Pascal?

    Well, this is one solution to this problem in two steps:
    1 - read all the lines from the file
    2 - write it in the file again without the first line
    If you want this inside a procedure this should look:

    procedure delfirst(s:string); { s is the filename }
    var
    fil:text; { the file }
    lines:array[1..100] of string; { the content }
    counter:byte; { this should count the lines }
    loop:byte; { this is a temporary loop control variable }
    begin
    counter:=0;
    assign(fil,s); { Open the file }
    reset(fil);
    repeat
    inc(counter);
    readln(fil,lines[counter]); { read lines from the file }
    until EOF(fil); { repeat until the END of file }
    close(fil); { close it }
    assign{fil,s}; { open the file for writing }
    rewrite(fil);
    for loop:=2 to counter do writeln(fil,lines[n]); { now write }
    close(fil); {and close it }
    end;

    I hope it works, its not the best solution, but its one. I hope you find it useful.

    GAASHIUS
  • zibadianzibadian Posts: 6,349Member
    : You have written:
    : > Any ideas how could I delete a first line of text file in Pascal?
    :
    : Well, this is one solution to this problem in two steps:
    : 1 - read all the lines from the file
    : 2 - write it in the file again without the first line
    : If you want this inside a procedure this should look:
    :
    : procedure delfirst(s:string); { s is the filename }
    : var
    : fil:text; { the file }
    : lines:array[1..100] of string; { the content }
    : counter:byte; { this should count the lines }
    : loop:byte; { this is a temporary loop control variable }
    : begin
    : counter:=0;
    : assign(fil,s); { Open the file }
    : reset(fil);
    : repeat
    : inc(counter);
    : readln(fil,lines[counter]); { read lines from the file }
    : until EOF(fil); { repeat until the END of file }
    : close(fil); { close it }
    : assign{fil,s}; { open the file for writing }
    : rewrite(fil);
    : for loop:=2 to counter do writeln(fil,lines[n]); { now write }
    : close(fil); {and close it }
    : end;
    :
    : I hope it works, its not the best solution, but its one. I hope you find it useful.
    :
    : GAASHIUS
    :
    The following is somewhat better, because it can handle any length of file:
    [code]
    var
    FIn, FOut: text;
    NumberOfLinesToDelete: integer;
    Line: string;
    begin
    NumberOfLinesToDelete := 1;
    Assign(FIn, 'SomeFileIn.txt');
    Assign(FOut, 'SomeFileOut.txt');
    Reset(FIn);
    Rewrite(FOut);
    while not eof(FIn) do
    begin
    readln(FIn, Line);
    if NumberOfLinesToDelete = 0 then
    writeln(FOut, Line);
    else
    dec(NumberOfLinesToDelete);
    end;
    Close(FOut);
    Close(FIn);
    end;
    [/code]
  • DrMartenDrMarten Posts: 748Member
    [b][red]This message was edited by DrMarten at 2005-3-18 19:28:20[/red][/b][hr]
    [b][red]This message was edited by DrMarten at 2005-3-18 19:21:59[/red][/b][hr]
    [b][red]This message was edited by DrMarten at 2005-3-18 19:21:13[/red][/b][hr]
    : : You have written:
    : : > Any ideas how could I delete a first line of text file in Pascal?
    : :
    : : Well, this is one solution to this problem in two steps:
    : : 1 - read all the lines from the file
    : : 2 - write it in the file again without the first line
    : : If you want this inside a procedure this should look:
    : :
    : : procedure delfirst(s:string); { s is the filename }
    : : var
    : : fil:text; { the file }
    : : lines:array[1..100] of string; { the content }
    : : counter:byte; { this should count the lines }
    : : loop:byte; { this is a temporary loop control variable }
    : : begin
    : : counter:=0;
    : : assign(fil,s); { Open the file }
    : : reset(fil);
    : : repeat
    : : inc(counter);
    : : readln(fil,lines[counter]); { read lines from the file }
    : : until EOF(fil); { repeat until the END of file }
    : : close(fil); { close it }
    : : assign{fil,s}; { open the file for writing }
    : : rewrite(fil);
    : : for loop:=2 to counter do writeln(fil,lines[n]); { now write }
    : : close(fil); {and close it }
    : : end;
    : :
    : : I hope it works, its not the best solution, but its one. I hope you find it useful.
    : :
    : : GAASHIUS
    : :
    : The following is somewhat better, because it can handle any length of file:
    : [code]
    : var
    : FIn, FOut: text;
    : NumberOfLinesToDelete: integer;
    : Line: string;
    : begin
    : NumberOfLinesToDelete := 1;
    : Assign(FIn, 'SomeFileIn.txt');
    : Assign(FOut, 'SomeFileOut.txt');
    : Reset(FIn);
    : Rewrite(FOut);
    : while not eof(FIn) do
    : begin
    : readln(FIn, Line);
    : if NumberOfLinesToDelete = 0 then
    : writeln(FOut, Line);
    : else
    : dec(NumberOfLinesToDelete);
    : end;
    : Close(FOut);
    : Close(FIn);
    : end;
    : [/code]
    :


    Why not just simply skip past the 1st line
    just using three variables with this which is similiar to above code but shorter ( will also do any length of file ) >


    var
    oldfile,newfile :text;
    line : string;
    Assign(oldfile, 'myoldfilename.txt');
    Assign(newfile, 'mynewfilename.txt');
    Begin
    Reset(oldfile);
    Rewrite(newfile);
    ReadLn (oldfile, line);
    Repeat
    ReadLn(oldfile, line);
    WriteLn(new file, line);
    Until EOF(oldfile);
    Close(oldfile);
    Close(newfile);
    End;

    This would write the newfile without the 1st line. :-)

  • zibadianzibadian Posts: 6,349Member
    : [b][red]This message was edited by DrMarten at 2005-3-18 19:28:20[/red][/b][hr]
    : [b][red]This message was edited by DrMarten at 2005-3-18 19:21:59[/red][/b][hr]
    : [b][red]This message was edited by DrMarten at 2005-3-18 19:21:13[/red][/b][hr]
    : : : You have written:
    : : : > Any ideas how could I delete a first line of text file in Pascal?
    : : :
    : : : Well, this is one solution to this problem in two steps:
    : : : 1 - read all the lines from the file
    : : : 2 - write it in the file again without the first line
    : : : If you want this inside a procedure this should look:
    : : :
    : : : procedure delfirst(s:string); { s is the filename }
    : : : var
    : : : fil:text; { the file }
    : : : lines:array[1..100] of string; { the content }
    : : : counter:byte; { this should count the lines }
    : : : loop:byte; { this is a temporary loop control variable }
    : : : begin
    : : : counter:=0;
    : : : assign(fil,s); { Open the file }
    : : : reset(fil);
    : : : repeat
    : : : inc(counter);
    : : : readln(fil,lines[counter]); { read lines from the file }
    : : : until EOF(fil); { repeat until the END of file }
    : : : close(fil); { close it }
    : : : assign{fil,s}; { open the file for writing }
    : : : rewrite(fil);
    : : : for loop:=2 to counter do writeln(fil,lines[n]); { now write }
    : : : close(fil); {and close it }
    : : : end;
    : : :
    : : : I hope it works, its not the best solution, but its one. I hope you find it useful.
    : : :
    : : : GAASHIUS
    : : :
    : : The following is somewhat better, because it can handle any length of file:
    : : [code]
    : : var
    : : FIn, FOut: text;
    : : NumberOfLinesToDelete: integer;
    : : Line: string;
    : : begin
    : : NumberOfLinesToDelete := 1;
    : : Assign(FIn, 'SomeFileIn.txt');
    : : Assign(FOut, 'SomeFileOut.txt');
    : : Reset(FIn);
    : : Rewrite(FOut);
    : : while not eof(FIn) do
    : : begin
    : : readln(FIn, Line);
    : : if NumberOfLinesToDelete = 0 then
    : : writeln(FOut, Line);
    : : else
    : : dec(NumberOfLinesToDelete);
    : : end;
    : : Close(FOut);
    : : Close(FIn);
    : : end;
    : : [/code]
    : :
    :
    :
    : Why not just simply skip past the 1st line
    : just using three variables with this which is similiar to above code but shorter ( will also do any length of file ) >
    :
    :
    : var
    : oldfile,newfile :text;
    : line : string;
    : Assign(oldfile, 'myoldfilename.txt');
    : Assign(newfile, 'mynewfilename.txt');
    : Begin
    : Reset(oldfile);
    : Rewrite(newfile);
    : ReadLn (oldfile, line);
    : Repeat
    : ReadLn(oldfile, line);
    : WriteLn(new file, line);
    : Until EOF(oldfile);
    : Close(oldfile);
    : Close(newfile);
    : End;
    :
    : This would write the newfile without the 1st line. :-)
    :
    :
    True, but the code I gave can without rewriting remove any number of lines at the start of the file. This makes it somewhat more flexible. It also is possible to let the user decide how many lines he wants to remove.
Sign In or Register to comment.