我现在要读取一个固定格式的.txt文本内容,然后写入到数据库中。但是当我根据字符的长度选择性写入到数据库时,总是出现长度的计算错误。
例如我判断‘你’和‘ni’时,应该长度为2。但是我的代码中显示的是‘?’和‘ni’就成了长度为1和2了。
delphi重新装了,还是不行……

解决方案 »

  1.   

    怎么显示的?string类型有时会无法识别汉字,用双字节字符串类型来显示吧
      

  2.   

    考虑用AnsiString的函数或WideStringhttp://lysoft.7u7.net
      

  3.   

    Postalcode:=trim(leftStr(str1,10));
    Address:=trim(Midstr(str1,11,50));
    Company:=trim(Midstr(str1,61,50));
    UserName:=trim(Midstr(str1,111,20));
      

  4.   

    字符集的问题该怎么处理啊,我不懂,望大家指教。
    txt文本中显示的字体是‘宋体’,但是原样拷到代码区就不显示了。
    大家帮帮啊,分不够可以再加。千万不要因此让我对Delphi失去了信心啊。
      

  5.   

    这是全部代码,大家帮帮我啊,我急死了。
    unit GetInfo;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Buttons, DB, ADODB,StrUtils;type
      TFrmGetInfo = class(TForm)
        Btn_GetInfo: TBitBtn;
        ADOTable1: TADOTable;
        Button1: TButton;
        ListBox1: TListBox;
        ListBox2: TListBox;
        ListBox3: TListBox;
        Memo1: TMemo;
        procedure Btn_GetInfoClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      FrmGetInfo: TFrmGetInfo;implementation{$R *.dfm}procedure TFrmGetInfo.Btn_GetInfoClick(Sender: TObject);
    var
      logfile:TextFile;
      logbak:TextFile;
      Str1:widestring;
      BatFile,TxtFile:wideString;
      UserName,Company,Address,Postalcode,B1,network:widestring;
    begin
       BatFile:=ExtractFilePath(Paramstr(0))+'post.bat';//»ñÈ¡post.batµÄ·¾¶
       TxtFile:=ExtractFilePath(Paramstr(0))+'Post.txt';//»ñÈ¡post.txtµÄ·¾¶
       AssignFile(logbak,BatFile);
       if not FileExists(BatFile)then
        Rewrite(logbak)
        else
           Append(logbak);
           if not FileExists(BatFile)then
              MessageDlg('Ä¿±êÎļþ²»´æÔÚ£¡',mtInformation,[mbOk],0)
           else
              begin
                 AssignFile(logfile,'post.txt');
                 Reset(logfile);
                 if Eof(logfile) then
                    begin
                       ShowMessage('ÈÕÖ¾Îļþ²»´æÔÚ£¡');
                       Exit;
                    end;
              end;
    //¶ÁÈ¡Îı¾ÄÚÈÝ£¬²¢Ð´Èëµ½Êý¾Ý¿âÖÐ
    memo1.Clear;
       while not Eof(logfile) do
         begin
           Readln(logfile,Str1);
           Writeln(logbak,str1);
           Postalcode:=trim(leftStr(str1,10));
           Address:=trim(AnsiMidstr(str1,11,50));
           Company:=trim(AnsiMidstr(str1,61,50));
           UserName:=trim(AnsiMidstr(str1,111,20));
           B1:=trim(AnsiMidstr(str1,131,10));
           network:=trim(Ansirightstr(str1,10));
           listbox1.Items.Add(username);
           listbox2.Items.Add(address);
           listbox3.Items.Add(company);
           with adotable1 do
           begin
             TableName:='UserInfo';
             Open;
             try
               begin
                Append;
                FieldByName('UserName').Value:=UserName;
                FieldByName('Postalcode').Value:=Postalcode;
                FieldByName('Address').Value:=Address;
                FieldByName('Company').Value:=Company;
                FieldByName('B1').Value:=B1;
                FieldByName('network').Value:=network;
                post;
               end;//try
            except
                cancel;
            end;//try...except
          end;//with }
         memo1.Lines.Add(str1);
       end; //while
       CloseFile(logfile);
       CloseFile(logbak);
       ShowMessage('³É¹¦¶ÁÈ¡ËùÓÐÊý¾Ý£¡');
    end;procedure TFrmGetInfo.FormCreate(Sender: TObject);
    begin
    adoTable1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+ExtractFilePath(Paramstr(0))+'UserInfo.mdb'+';Persist Security Info=False'
    end;
    end.
      

  6.   

    我给你测试了下,没什么问题啊,看的代码
    我text内容是‘大家好我是阿杜,你叫什么名字啊,我也叫阿杜,啊,,怎么搞啊,什么意思啊’
    最后显示结果是“家好我是”,没什么出错啊
    procedure TForm1.Button1Click(Sender: TObject);
    var
      LogFile:TextFile;
      TempStr:WideString;
      UserName,Company,Address,Postalcode:widestring;
    begin
      AssignFile(LogFile,'c:\test.txt');
      Reset(LogFile);
      try
        while not eof(LogFile) do
        begin
          ReadLn(logFile,Tempstr);
        end;
      Finally
        CloseFile(logFile);
      end;
      Postalcode:=trim(leftStr(tempstr,1));
      Address:=trim(AnsiMidstr(tempstr,2,4));
      Company:=trim(AnsiMidstr(tempstr,6,10));
      UserName:=trim(AnsiMidstr(tempstr,111,20));
      showmessage(address);
    end;
      

  7.   

    解决不了,只能转换成excel再读入到数据库中了。他写入到txt是用的pb,我用vb和delphi都不能判断它长度,总是出错。谢谢大家了。