文本文件内容的处理文本文件有1000多个字符
需求如下:
1.对文本文件重新排版(一行保证20个字符)
2.对排版后的内容进行每行分别处理
查看每行的第10个字符,如果该字符为0则 将每行的第10个字符转换成'a'
如果该字符不为0则 将此行的数据删除掉如:a.txt内容为:(假设第一步排版已经搞定)abcde001100abcde0011
ffddfdsfdsfds01safdf
zzzz22z2zzzzzzzzzzzz
kkkk0000000000000000则正确结果应为:abcde001100abcde0011
kkkk0000000000000000
需求如下:
1.对文本文件重新排版(一行保证20个字符)
2.对排版后的内容进行每行分别处理
查看每行的第10个字符,如果该字符为0则 将每行的第10个字符转换成'a'
如果该字符不为0则 将此行的数据删除掉如:a.txt内容为:(假设第一步排版已经搞定)abcde001100abcde0011
ffddfdsfdsfds01safdf
zzzz22z2zzzzzzzzzzzz
kkkk0000000000000000则正确结果应为:abcde001100abcde0011
kkkk0000000000000000
AStr: tstrings;
i: integer;
readStr: string;
begin
try
astr := Tstringlist.create;
astr.loadfromfile(Afilename);
for i := astr.count -1 downto 0 do
begin
astr[i] := copy(astr[i],1,20);
if astr[i][10]='0' then
astr[i][10] := 'a'
else
astr.delete[i];
end;
astr.SaveToFile(afilename); finally
astr.free;
end;end;
file1:Textfile;
file2:Tstringlist;
v_str,v_str2:string;
i,j:integer;
begin
file2:= Tstringlist.Create;
OpenDialog1.InitialDir:=ExtractFilePath(application.ExeName);
if OpenDialog1.Execute then
begin
assignfile(file1,OpenDialog1.FileName);
reset(file1);
while not system.Eof(file1) do
begin
readln(file1,v_str);
v_str:=copy(v_str,1,20);
if copy(v_str,10,1)='0' then
v_str:=copy(v_str,1,9)+'a'+copy(v_str,11,10)
else v_str:='';
if v_str<>'' then
file2.add(v_str);
end;
end;
closefile(file1);
file2.SaveToFile(OpenDialog1.FileName);
end;
ofile : textfile;
nfile : textfile;
i, j : integer;
ostr : string;
nstr : string;
filename: string;
begin
if opendialog1.Execute then
filename := opendialog1.filename;
assignfile(ofile, filename);
assignfile(nfile, filename + '1');
reset(ofile);
append(nfile);
i := 0; j := 19;
while not eof(ofile) do
begin
readln(nfile, ostr);
while j <= length(ostr) do
begin
nstr := copy(ostr, i, j);
if copy(nstr, 9, 1) = '0' then
begin
nstr := copy(nstr, 0, 8) + 'a' + copy(nstr, 10, length(nstr));
writeln(nfile, nstr);
end
else nstr := '';
i := i + j + 1;
j := j + 20;
ostr := copy(ostr, i, j);
end;
end;
end;
var
file1,w2:Textfile;
file2:Tstringlist;
v_str,v_str2:string;
i,j:integer;
begin
i:=0;
file2:= Tstringlist.Create;
OpenDialog1.InitialDir:=ExtractFilePath(application.ExeName);
if OpenDialog1.Execute then
assignfile(file1,OpenDialog1.FileName);
reset(file1);
while not system.Eof(file1) do
begin
readln(file1,v_str);
file2.add(v_str);
end;
closefile(file1);
assignfile(w2,'C:\aa.txt');
Rewrite(w2);
i:=0;
if ((length(file2.Text)) mod 20)>0 then
j:=((length(file2.Text)) div 20)+1
else
J:=((length(file2.Text)) div 20);
while (i<=j) do
begin
if i=0 then
v_str:=copy(file2.Text,1,20)
else
v_str:=copy(file2.Text,i,20);
if copy(v_str,10,1)='0' then
v_str:=copy(v_str,1,9)+'a'+copy(v_str,11,10)
else
v_str:='';
if v_str<>'' then
Writeln(v_str);
i:=i+20;
end;
closeFile(w2);
file1:Textfile;
file2:Tstringlist;
v_str,v_str2:string;
i,j:integer;
begin
file2:= Tstringlist.Create;
OpenDialog1.InitialDir:=ExtractFilePath(application.ExeName);
if OpenDialog1.Execute then
begin
assignfile(file1,OpenDialog1.FileName);
reset(file1);
while not system.Eof(file1) do
begin
readln(file1,v_str);
v_str:=copy(v_str,1,20);
if copy(v_str,10,1)='0' then
v_str:=copy(v_str,1,9)+'a'+copy(v_str,11,10)
else v_str:='';
if v_str<>'' then
file2.add(v_str);
end;
end;
closefile(file1);
file2.SaveToFile(OpenDialog1.FileName);
end;