Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
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.

need help with textfiles (2)

joelem18joelem18 Posts: 31Member
I have a text file which has some html constructs. I have to copy this text file into a new one but without any comments (ie. if there is <--hello-->, it has to be removed from the text file. How can I do that?

Comments

  • ManningManning Posts: 1,621Member
    : I have a text file which has some html constructs. I have to copy this text file into a new one but without any comments (ie. if there is <--hello-->, it has to be removed from the text file. How can I do that?
    :

    Heres what I would do:

    Have a boolean called InComment initially set to false.
    Have a loop calling ReadLn to read the file line by line.
    After reading the line have an if statement that does either:

    1) If InComment is true, check for "-->". If it isnt found, ignore the whole string since its a comment. If it is found, delete all the text up to the "-->" and set InComment to false and write whats left of the string.

    2) If InComment is false, check for "". If it is found, delete the text between the two sequences and write whats left of the string. If "-->" isnt found, set InComment to true and erase from " blah blah blah

    And that should be all thats necessary. It was probably harder to try to describe the steps involved than it would have been to just write the pascal program, but I thought I would leave it to you to attempt first. If you still have problems just post what you have.
  • joelem18joelem18 Posts: 31Member
    I tried doing what you told me but I didn't manage. Can you please send me the code so that i could know what I'm doing wrong please?

  • ManningManning Posts: 1,621Member
    : I tried doing what you told me but I didn't manage. Can you please send me the code so that i could know what I'm doing wrong please?

    It probably wont help much because I wrote it in a rush and it's incredibly messy, but this works fine here:

    [code]
    program a;

    uses
    crt;

    var
    inf, outf: text;
    s: string;
    comm: boolean = false;

    begin
    assign(inf, 'comm.html');
    reset(inf);
    assign(outf, 'nocomm.html');
    rewrite(outf);

    while not eof(inf) do
    begin
    readln(inf, s);
    if (comm) and (pos('-->', s) = 0) then
    s := '';
    while (pos('', s) > 0) do
    begin
    if (comm) then
    begin
    if (pos('-->', s) > 0) then
    begin
    delete(s, 1, pos('-->', s) + 2);
    comm := false;
    end else
    begin
    s := '';
    end;
    end else
    begin
    if (pos('', s) > 0) then
    begin
    delete(s, pos('', s) + 2 - pos('<!--', s) + 1);
    end else
    begin
    delete(s, pos('<!--', s), length(s));
    comm := true;
    end;
    end;
    end;
    end;
    if (s <> '') then
    writeln(outf, s);
    end;

    close(inf);
    close(outf);
    end.
    [/code]
  • joelem18joelem18 Posts: 31Member
    [b][red]This message was edited by Moderator at 2002-11-23 10:32:3[/red][/b][hr]
    I did exactly what you told me but no luck....my program is crashing. Here is what i wrote in my program and also here's my textfile.

    Test.PAS
    ------------------
    [code]
    Program Test;

    var InFile, Outfile : Text;
    Line : string;
    Command : boolean;


    begin
    {Prepare file for input and output}
    Assign(InFile, 'C:sample.HTM');
    Reset(InFile); {Open InFile for input}

    Assign(OutFile, 'C:copy.HTM');
    Rewrite(OutFile); {Open Outfile for output}

    {Copy the data line by line}
    while not EOF(InFile) do
    begin
    Command := False;
    ReadLn(InFile, Line);
    if Command and (pos('-->', Line) = 0) then
    Line := '';
    while (pos('<--', Line) > 0) or (pos('-->', Line) > 0) do
    begin
    if Command then
    begin
    if (pos('-->', Line) > 0) then
    begin
    delete(Line, 1, pos('-->', Line) + 2);
    Command := False;
    end;
    end;
    end;
    end;
    if (Line <> '') then
    WriteLn(OutFile, Line);

    {Close file}
    Close(InFile);
    Close(OutFile)
    End.
    [/code]

    Sample.HTM
    --------------------------
    <?html version="1.0">
    <-- this is a comment -->











    <-- This is a comment -->
    viewpoint
    {
    from <0,0,-1>
    at <0,0,5>
    up <0,1,0>
    }

    object
    {
    sphere <0,0,5>,2
    shiny_red
    }

    light <5,5,5>



  • ManningManning Posts: 1,621Member
    [b][red]This message was edited by Moderator at 2002-11-23 10:39:30[/red][/b][hr]
    : I did exactly what you told me but no luck....my program is crashing. Here is what i wrote in my program and also here's my textfile.

    You didnt do exactly what I told you, you cut out over half of the program. Without knowing what error message you are getting I wont know what is causing the crash, but even if you fix the crash I can tell you that what you have there will not work at all.

    Actually I just looked at the code again...from what I can see the program shouldnt crash, but it definitely will be thrown into an infinite loop. My suggestion would be to either use my code in its entirety since I know for a fact it'll work, or to read through and fully understand the logic of it before you cut parts out. That's where your problem lies.
Sign In or Register to comment.