各位前辈 我中午提的问题谢谢你们帮我想办法 我把100分全送给你们了,请查收
不过下午问题具体化了 
希望好心的前辈指点我一下:1文本用户  帐号  交易次数  交易金额
A     001      2        1000
B     102      3        2000
。2文本
用户  帐号  交易次数  交易金额
A     001      3        2000
C     201      2        1000
......
怎么样实现1文本和2文本的累加,重复的A交易次数和金额累加到3
不重复的B,C生成新的一行记录到
3文本
用户  帐号  交易次数  交易金额
A     001     5       3000
B     102     3       2000
C     201     2       1000需要具体解决问题的代码,控件用2个OPENDILOGE选择文本,按钮运行。谢谢,各列都是定长的。谢谢!100分相送!

解决方案 »

  1.   

    设计了一个类TFileUnion,应该能满足LZ的要求:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;  TFileUnion = class(TObject)
      private
        FColName, FColNumber, FColValue: TStringList;
        constructor Create;
        destructor Destroy;
      public
        procedure AddFile(sFilename: String);
        procedure Output(sFilename: String);
      end;var
      Form1: TForm1;implementation{$R *.dfm}constructor TFileUnion.Create;
    begin
      FColName := TStringList.Create;
      FColNumber := TStringList.Create;
      FColValue := TStringList.Create;
    end;destructor TFileUnion.Destroy;
    begin
      FColName.Free;
      FColNumber.Free;
      FColValue.Free;
    end;procedure TFileUnion.AddFile(sFilename: String);
    var
      FileHandle : TextFile;
      sLine      : String;
      sName      : String;
      sNumber    : String;
      sValue     : String;
      iIndex     : Integer;
    begin
      AssignFile(FileHandle, sFilename);
      {$I-}
      Reset(FileHandle);
      {$I+}
      if IOResult = 0 then
      begin
        Readln(FileHandle, sLine);
        while not Eof(FileHandle) do
        begin
          Readln(FileHandle, sLine);
          sName := Copy(sLine, 1, 10);
          sNumber := Trim(Copy(sLine, 11, 10));
          sValue := Trim(Copy(sLine, 21, 10));
          if FColName.Find(sName, iIndex) then
          begin
            FColNumber.Strings[iIndex] := IntToStr(StrToInt(sNumber) +
                                          StrToInt(FColNumber.Strings[iIndex]));
            FColValue.Strings[iIndex] := IntToStr(StrToInt(sValue) +
                                          StrToInt(FColValue.Strings[iIndex]));
          end
          else begin
            FColName.Add(sName);
            FColNumber.Add(sNumber);
            FColValue.Add(sValue);
          end;
        end;
        Close(FileHandle);
      end;
    end;procedure TFileUnion.Output(sFilename: String);
    var
      FileHandle : TextFile;
      iLoop      : Integer;
    begin
      AssignFile(FileHandle, sFilename);
      {$I-}
      Rewrite(FileHandle);
      {$I+}
      if IOResult = 0 then
      begin
        Writeln(FileHandle, '用户  帐号  交易次数  交易金额');
        for iLoop := 0 to FColName.Count - 1 do
        begin
          Write(FileHandle, FColName.Strings[iLoop]);
          Write(FileHandle, Copy('     ' + FColNumber.Strings[iLoop] + '      ', 1, 10));
          Writeln(FileHandle, Copy('    ' + FColValue.Strings[iLoop] + '      ', 1, 10));
        end;
        Close(FileHandle);
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      AFile : TFileUnion;
    begin
      AFile := TFileUnion.Create;
      try
        //这里没有调用OpenDialog,而是直接写的文件名常数
        //相信楼主自己能修改的
        AFile.AddFile('c:\1.txt');
        AFile.AddFile('c:\2.txt');
        AFile.Output('c:\3.txt');
      finally
        AFile.Free;
      end;
    end;end.
      

  2.   

    大家帮我看一下啊,为什么生成的3文件是空白的,没有数据啊unit hebing;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;  TFileUnion = class(TObject)
      private
      FColabbr_mcht_name, FColmcht_id, FColacount_bank_code,FColaccount_number,FColcount_of_transaction,
      FColamount_of_transaciton,FColfee,FColcount_of_adjust,FColamount_of_adjust,FColnet_amount,FColadj_fee : TStringList;
      constructor Create;
      destructor Destroy;
      public
      procedure AddFile(sFilename: String);
      procedure Output(sFilename: String);
    end;var
      Form1: TForm1;implementation{$R *.dfm}
    constructor TFileUnion.Create;
    begin
      FColabbr_mcht_name:= TStringList.Create;
      FColmcht_id:= TStringList.Create;
      FColacount_bank_code:= TStringList.Create;
      FColaccount_number:= TStringList.Create;
      FColcount_of_transaction:= TStringList.Create;
      FColamount_of_transaciton:= TStringList.Create;
      FColfee:=TStringList.Create;
      FColcount_of_adjust:=TStringList.Create;
      FColamount_of_adjust:= TStringList.Create;
      FColnet_amount:=TStringList.Create;
      FColadj_fee:=TStringList.Create;
    end;destructor TFileUnion.Destroy;
    begin
      FColabbr_mcht_name.Free;
      FColmcht_id.Free;
      FColacount_bank_code.Free;
      FColaccount_number .Free;
      FColcount_of_transaction.Free;
      FColamount_of_transaciton.Free;
      FColfee.Free;
      FColcount_of_adjust.Free;
      FColamount_of_adjust.Free;
      FColnet_amount.Free;
      FColadj_fee.Free;
    end;procedure TFileUnion.AddFile(sFilename: String);
    var
      FileHandle : TextFile;
      sLine      : String;
      sabbr_mcht_name: String;
      smcht_id: String;
      sacount_bank_code: String;
      saccount_number : String;
      scount_of_transaction: String;
      samount_of_transaciton: String;
      sfee: String;
      scount_of_adjust: String;
      samount_of_adjust: String;
      snet_amount: String;
      sadj_fee: String;
      iIndex     : Integer;
    begin
      AssignFile(FileHandle, sFilename);
      {$I-}
      Reset(FileHandle);
      {$I+}
      if IOResult = 0 then
      begin
        Readln(FileHandle, sLine);
        while not Eof(FileHandle) do
        begin
          Readln(FileHandle, sLine);
          sabbr_mcht_name := Copy(sLine, 1, 8);
          smcht_id := Trim(Copy(sLine, 10, 24));
          sacount_bank_code:= Trim(Copy(sLine, 26, 33));
          saccount_number := Trim(Copy(sLine, 35, 64));
          scount_of_transaction:= Trim(Copy(sLine, 66, 75));
          samount_of_transaciton:= Trim(Copy(sLine, 77, 88));
          sfee:= Trim(Copy(sLine, 90, 101));
          scount_of_adjust:= Trim(Copy(sLine, 103, 112));
          samount_of_adjust:= Trim(Copy(sLine, 114,125));
          snet_amount:= Trim(Copy(sLine, 127, 138));
          sadj_fee:= Trim(Copy(sLine, 140, 151));
          if FColabbr_mcht_name.Find(sabbr_mcht_name , iIndex) then
          begin
            FColcount_of_transaction.Strings[iIndex] := IntToStr(StrToInt(scount_of_transaction) +
                                          StrToInt(FColcount_of_transaction.Strings[iIndex]));
            FColamount_of_transaciton.Strings[iIndex] := IntToStr(StrToInt(samount_of_transaciton) +
                                          StrToInt(FColamount_of_transaciton.Strings[iIndex]));
            FColfee.Strings[iIndex] := IntToStr(StrToInt(sfee) +
                                          StrToInt(FColfee.Strings[iIndex]));
            FColcount_of_adjust.Strings[iIndex] := IntToStr(StrToInt(scount_of_adjust) +
                                          StrToInt(FColcount_of_adjust.Strings[iIndex]));
            FColamount_of_adjust.Strings[iIndex] := IntToStr(StrToInt(samount_of_adjust) +
                                          StrToInt(FColamount_of_adjust.Strings[iIndex]));
            FColnet_amount.Strings[iIndex] := IntToStr(StrToInt(snet_amount) +
                                          StrToInt(FColnet_amount.Strings[iIndex]));
            FColadj_fee.Strings[iIndex] := IntToStr(StrToInt(sadj_fee) +
                                          StrToInt(FColadj_fee.Strings[iIndex]));
          end
          else begin
            FColabbr_mcht_name .Add(sabbr_mcht_name );
            FColmcht_id.Add(smcht_id);
            FColacount_bank_code.Add(sacount_bank_code);
            FColaccount_number.Add(saccount_number);
            FColcount_of_transaction.Add(scount_of_transaction);
            FColamount_of_transaciton.Add(samount_of_transaciton);
            FColfee.Add(sfee);
            FColcount_of_adjust.Add(scount_of_adjust);
            FColamount_of_adjust.Add(samount_of_adjust);
            FColnet_amount.Add(snet_amount);
            FColadj_fee.Add(sadj_fee);
          end;
        end;
        Close(FileHandle);
      end;
    end;procedure TFileUnion.Output(sFilename: String);
    var
      FileHandle : TextFile;
      iLoop      : Integer;
    begin
      AssignFile(FileHandle, sFilename);
      {$I-}
      Rewrite(FileHandle);
      {$I+}
      if IOResult = 0 then
      begin
        Writeln(FileHandle, '商户简称        商户代码 开户代码                   商户入帐帐号   交易笔数     交易金额   应扣服务费   调整笔数     调整金额     净入帐额   调整服务费');
        for iLoop := 0 to FColabbr_mcht_name.Count - 1 do
        begin
          Write(FileHandle, FColabbr_mcht_name.Strings[iLoop]);
          Writeln(FileHandle, Copy('               ' + FColmcht_id.Strings[iLoop] + '               ', 1, 15));
          Writeln(FileHandle, Copy('    ' + FColacount_bank_code.Strings[iLoop] + '      ', 1, 8));
          Writeln(FileHandle, Copy('    ' + FColaccount_number.Strings[iLoop] + '      ', 1, 30));
          Writeln(FileHandle, Copy('    ' + FColcount_of_transaction.Strings[iLoop] + '      ', 1, 10));
          Writeln(FileHandle, Copy('    ' + FColamount_of_transaciton.Strings[iLoop] + '      ', 1, 12));
          Writeln(FileHandle, Copy('    ' + FColfee.Strings[iLoop] + '      ', 1, 12));
          Writeln(FileHandle, Copy('    ' + FColcount_of_adjust.Strings[iLoop] + '      ', 1, 10));
          Writeln(FileHandle, Copy('    ' + FColamount_of_adjust.Strings[iLoop] + '      ', 1, 12));
          Writeln(FileHandle, Copy('    ' + FColnet_amount.Strings[iLoop] + '      ', 1, 12));
          Writeln(FileHandle, Copy('    ' + FColadj_fee.Strings[iLoop] + '      ', 1, 12));
        end;
        Close(FileHandle);
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      AFile : TFileUnion;
    begin
      AFile := TFileUnion.Create;
      try
        AFile.AddFile('d:\1.txt');
        AFile.AddFile('d:\2.txt');
        AFile.Output('d:\3.txt');
      finally
        AFile.Free;
      end;
    end;end.
      

  3.   

    最大的可能是'd:\1.txt'和'd:\2.txt'这两个文件不存在
      

  4.   

    你的代码我测试了一下, 测试时我是这样做的:  AFile := TFileUnion.Create;
      try
        AFile.AddFile('d:\1.txt');
        AFile.Output('d:\3.txt');
      finally
        AFile.Free;
      end;这样测试是为了避免了AddFile进行合并的操作, 结果发现Output是正常的。
      

  5.   

    abbr^mcht^name[8]  name            商户简称
    mcht^id[15]        id            商户代码
    acount^bank^code[8]  banknumber           开户行代码
    account^number [30]  number          商户入帐帐号
    count^of^transaction[10]    count_t   交易笔数(总分核对栏位)
    amount^of^transaciton[12]   amount_t       交易金额(总分核对栏位)
    fee[12] fee     应扣服务费
    count^of^adjust[10]         count_a       调整笔数
    amount^of^adjust[12]        amount_a   调整金额(包括退货,结算交易)
    net^amount[12]              amout_n   净入帐额
    adj^fee[12]                 fee_a      调整服务费
    这11项 要累加后7项的数据
      

  6.   

    是不是这段函数有问题啊 字符串都是定长的    begin
          Write(FileHandle, FColabbr_mcht_name.Strings[iLoop]);
          Writeln(FileHandle, Copy('               ' + FColmcht_id.Strings[iLoop] + '               ', 1, 15));
          Writeln(FileHandle, Copy('    ' + FColacount_bank_code.Strings[iLoop] + '      ', 1, 8));
          Writeln(FileHandle, Copy('    ' + FColaccount_number.Strings[iLoop] + '      ', 1, 30));
          Writeln(FileHandle, Copy('    ' + FColcount_of_transaction.Strings[iLoop] + '      ', 1, 10));
          Writeln(FileHandle, Copy('    ' + FColamount_of_transaciton.Strings[iLoop] + '      ', 1, 12));
          Writeln(FileHandle, Copy('    ' + FColfee.Strings[iLoop] + '      ', 1, 12));
          Writeln(FileHandle, Copy('    ' + FColcount_of_adjust.Strings[iLoop] + '      ', 1, 10));
          Writeln(FileHandle, Copy('    ' + FColamount_of_adjust.Strings[iLoop] + '      ', 1, 12));
          Writeln(FileHandle, Copy('    ' + FColnet_amount.Strings[iLoop] + '      ', 1, 12));
          Writeln(FileHandle, Copy('    ' + FColadj_fee.Strings[iLoop] + '      ', 1, 12));