文本文件内容的处理文本文件有1000多个字符
需求如下:
1.对文本文件重新排版(一行保证20个字符)
2.对排版后的内容进行每行分别处理
查看每行的第10个字符,如果该字符为0则   将每行的第10个字符转换成'a'
                     如果该字符不为0则 将此行的数据删除掉如:a.txt内容为:(假设第一步排版已经搞定)abcde001100abcde0011
ffddfdsfdsfds01safdf
zzzz22z2zzzzzzzzzzzz
kkkk0000000000000000则正确结果应为:abcde001100abcde0011
kkkk0000000000000000

解决方案 »

  1.   

    var
      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;
      

  2.   

    var
      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;
      

  3.   

    小別的算法有問題啊,這樣做必須保証排版前的.txt文件沒行有20個字符啊!
      

  4.   

    小別的算法有問題啊,這樣做必須保証排版前的.txt文件每行有20個字符啊!但是要求去的排版後的.txt文件每行有20個字符!
      

  5.   

    var
      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;
      

  6.   

    思路之一:先把txt文件讀到TStringlist,在處理TStringlist,再寫到文本文件,如下:
    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);
      

  7.   

    var
      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;