主题:看看哪里错了?????/
[em6][em6] 例5 读入一个行长不定的文本文件。排版,建立一个行长固定为60个字符的文件, 排版要求:(1)当行末不是一个完整单词时,行最后一个字符位用'-'代替, 表示与下一行行头组成完整的单词;(2)第一行行头为两个空格,其余各行行头均不含有空格。
分析
(1)建立原始数据文件。
(2)程序边读入原始数据文件内容,边排版。
(3)每排完一行行长为60字符,并符合题中排版条件,写入目标文件中。
设原始数据TEXTCOPY.DAT文件内容如下:
Pavel was arrested.
That dat Mother did not light the stove.
Evening came and a cold wind was blowing.
There was a knock at the window.
Then another.
Mother was used to such knocks,but this time she gave a little start of joy.
Throwing a shawl over her shoulders,she opened the door.
程序:
const
maxlen=60;
var
f1,f2:text;
ch,ch1:char;
s:string[maxlen];
i,j:integer;
innname,outname:string[20];
begin
write('input texti filename');
readln(inname);
assign(f1,inname);
reset(f1);
write('input tetout filename');
readln(outname);
assign(f2,outname);
rewrite(f2);
j:=0; s:=' ';
while not eof(f1) do begin
while not eoln(f1) do begin
inc(j);
read(f1,ch);
if j=maxlen+1 then
case ch of
' ':begin writeln(f2,s);s:=' ';j:=0; end;
'.':begin writeln(f2,s);j;=1; s:=ch; end;
else begin
ch1:=s[60];
if s[59]=' ' then s[60]:=' ' else s[60]:='-';
writeln(f2,s); j:=2; s:=ch1+ch;
end;
end;
else s:=s+ch;
end;
writeln(f2,s);
close(f1); close(f2);
end.
[em6][em6][em6]
分析
(1)建立原始数据文件。
(2)程序边读入原始数据文件内容,边排版。
(3)每排完一行行长为60字符,并符合题中排版条件,写入目标文件中。
设原始数据TEXTCOPY.DAT文件内容如下:
Pavel was arrested.
That dat Mother did not light the stove.
Evening came and a cold wind was blowing.
There was a knock at the window.
Then another.
Mother was used to such knocks,but this time she gave a little start of joy.
Throwing a shawl over her shoulders,she opened the door.
程序:
const
maxlen=60;
var
f1,f2:text;
ch,ch1:char;
s:string[maxlen];
i,j:integer;
innname,outname:string[20];
begin
write('input texti filename');
readln(inname);
assign(f1,inname);
reset(f1);
write('input tetout filename');
readln(outname);
assign(f2,outname);
rewrite(f2);
j:=0; s:=' ';
while not eof(f1) do begin
while not eoln(f1) do begin
inc(j);
read(f1,ch);
if j=maxlen+1 then
case ch of
' ':begin writeln(f2,s);s:=' ';j:=0; end;
'.':begin writeln(f2,s);j;=1; s:=ch; end;
else begin
ch1:=s[60];
if s[59]=' ' then s[60]:=' ' else s[60]:='-';
writeln(f2,s); j:=2; s:=ch1+ch;
end;
end;
else s:=s+ch;
end;
writeln(f2,s);
close(f1); close(f2);
end.
[em6][em6][em6]