My task is to create a program which would take a text file as an input and write out all the words starting with 'a' to another one. The best solution I can come up with is as follows:
program project1;
var
f,g:text;
ch,b:char;
ile,pos1,pos2,i:integer;
s:string;
begin
assign(f, 'input.txt');
assign(g, 'score.txt');
reset(f);
rewrite(g);
ile:=0;
pos1:=0; pos2:=0;
while not eof(f) do begin
read(f,b);
write(g,b);
Inc(pos1);
read(f,ch);
write(g,ch);
Inc(pos2);
if (b=' ') and (ch='a') then begin
repeat
read(f,ch);
inc(pos2);
write(g,'ala');
until ch=' ';
//for i:=pos1 to pos2 do
//writeln(g,s[i]);
//writeln;
end;
end;
close(f);
close(g);
end.
When I run this it reproduces the contents of input.txt and places them in score.txt. I added random statement in my 'if condition' so I could see whether it ever evaluated to true and it turned out it didn't. Could you please come up with any clue what I'm doing wrong or maybe some solution?
EDIT
I tried to experiment by using the simplest example - I made an input.txt consisting of just one word, say 'export' and changed 'if condition' to :
if (b='e') and (ch='x') then begin
repeat
write(g,ch);
read(f,ch);
until ch='t';
My understanding is it should produce an output.txt consisting of just one word : 'export' but it seems to go into an infinite loop creating thousands of random characters in my output file...