Howdy, Stranger!

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

Categories

File opening

zsolt_93zsolt_93 Member Posts: 9
uses crt;
Function nev:string;
var st1:string[20];
ch:char;
begin
repeat
ch:=readkey;
st1:=st1+ch;
write(ch);
until ch=#32;
nev:=st1
end;
var name:string;
Begin
name:=nev;
assign(output,name);
rewrite(output);
End.

it says file not open for output.
If I put 'a.txt' instead of name it works.
So what is the problem?

Comments

  • AtexAtex Member Posts: 268
    [code]
    : uses crt;

    : Function nev:string;
    : var st1:string[20];
    : ch:char;
    : begin
    st1:=''; {Initialize st1, by default could be full of garbage}
    : repeat
    : ch:=readkey;
    : if ch>#32 then begin
    st1:=st1+ch; {This way will not append the space @ the end}
    : write(ch); {Echo only when ch is added to name}
    end;
    : until ch=#32; {Input will terminate upon hitting Space; Enter is: #13}
    : nev:=st1
    : end;

    : var name:string;
    : Begin
    : name:=nev;
    : assign(output,name);
    {$I-} {I/O checking OFF}
    : rewrite(output);
    {$I+}
    if ioresult<>0 then halt; {Will end program if unable to open}
    {ioresult will have a non zero error code if something gone wrong}
    : End.
    [/code]

  • ActorActor Member Posts: 447 ✭✭
    Atex's observation that you are not initializing [b]st1[/b] to a null string is the heart of your problem. You are appending your input to a bunch of garbage.

    Two questions.

    1. Why are you writing this function at all? Why not just use [italic]readln(name);[/italic] ?

    2. Why are you terminating your input with a space instead of #13, the ENTER key? Surely the ENTER key is more intuitive for the user. I repeatedly hit ENTER instead of SPACE, with the resulting string containing a, probably unwanted, #13 in the result.

    I suggest you use a [b]while[/b] loop instead of [b]repeat .. until[/b]. Use of [b]repeat .. until[/b] is the reason the terminating SPACE becomes part of your string. A [b]while[/b] loop eliminates this effect without the need of an extra test.
    [code]
    Function nev : string ;
    var
    st1 : string[20] ;
    ch : char;
    begin
    st1 := '' ; [red]{ initialize to null string }[/red]
    ch := readkey ; [red]{ extra assignment does not cost much }[/red]
    while ch <> #13 do begin
    st1 := st1 + ch ;
    write(ch) ;
    ch := readkey
    end ;
    writeln ; [red]{ good idea to put cursor on next line }[/red]
    nev:=st1
    end;
    [/code]
    if the extra assignment statement bothers you then use a infinite loop with [b]break[/b].
    [code]
    Function nev : string ;
    var
    st1 : string[20] ;
    ch : char;
    begin
    st1 := '' ; [red]{ initialize to null string }[/red]
    while TRUE do begin
    ch := readkey ;
    if ch = #13 then
    break ;
    st1 := st1 + ch ;
    write(ch)
    end ;
    writeln ; [red]{ good idea to put cursor on next line }[/red]
    nev:=st1
    end;
    [/code]

  • zsolt_93zsolt_93 Member Posts: 9
    Thanks for the help.
    I don't know where my mind was when i wanted to do that function instead of readln(name).
Sign In or Register to comment.