typing program

Hi, i'm trying to write a typing program with pascal, in this program, the user is free to type as much as he wants, and when he feels like it he saves as a text file, however, i'm stuck in commanding the program to leave the user typing freely.(This program should become a lot like the notepad), please, have you got any ideas on how i can do this? thanks, help is greatly appreciated.

Comments

  • Here's a small program to demonstrate how to do it, has quite a few limitations though, its far from being a notepad replacement...[code][color=Blue]{ Compiler: FPC 2.2.2 }
    {$type FPC}
    {$apptype console}


    program typing_demo;

    uses crt;

    const file_name='typing_demo.txt';


    var buf:array[0..$3fff] of char; // 16k of text buffer, about 8 full pages of text
    i:word; // buffer index


    procedure run;
    var ps:byte;
    ch:char;
    begin
    ps:=1;
    i:=0;
    repeat
    ch:=readkey;
    if (i>=($3fff-4)) then writeln(#13#10' <<< Buffer full >>> ') else
    case ch of
    #13:{enter}begin
    buf[i ]:=#13;
    buf[i+1]:=#10; { CR + LF }
    writeln;
    inc(i,2);
    ps:=1;
    end;
    #8:{backspace}if ps>1 then begin
    write(#8#32#8); // <-- same as: GotoXY(WhereX-1,WhereY);Write(' ');GotoXY(WhereX-1,WhereY);
    dec(ps);
    dec(i);
    end else write(#7); { beep }
    #32..#127:if ps>=79 then begin
    buf[i ]:=ch;
    buf[i+1]:=#13;
    buf[i+2]:=#10; { CR + LF }
    writeln(ch);
    inc(i,3);
    ps:=1;
    end else begin
    write(ch);
    buf[i]:=ch;
    inc(ps);
    inc(i);
    end;
    end;{case}
    until ch=#27; { Escape key }
    writeln;
    end;


    procedure save;
    var f:file;
    bw:word;
    begin
    writeln;
    if i>0 then begin
    assign(f,file_name);
    {$I-}rewrite(f,1);{$I+}
    if ioresult<>0 then begin
    writeln('Access denied or Read-only media');
    halt(1);
    end;
    {$I-}blockwrite(f,buf,i,bw);{$I+}
    if ioresult<>0 then begin
    writeln('Write error');
    halt(2);
    end;
    if i<>bw then writeln(bw,' byte(s) of ',i,' written to: ',file_name)
    else writeln(bw,' byte(s) written to: ',file_name);
    close(f);
    end else writeln('Nothing to save...');
    end;

    //######## Main ################################################################
    begin
    writeln(#13#10'Start typing, press ESC to exit and save'#13#10);
    run;
    save;
    end.
    [/color][/code]
Sign In or Register to comment.

Howdy, Stranger!

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

Categories

In this Discussion