txt文件操作问题?(100分)

  • 主题发起人 主题发起人 QXCOMM
  • 开始时间 开始时间
Q

QXCOMM

Unregistered / Unconfirmed
GUEST, unregistred user!
"13004642216","jine@cnuninet.com","金拥政",780.00
如何将:
Edit1.text:=13004642216
Edit2.text:=jine@cnuninet.com
Edit3.text:=金拥政
Edit4.text:=780.00
 

pos(",",,,,,,,,)
 
str:string;
str:="13004642216","jine@cnuninet.com","金拥政",780.00;
Edit1.text:=copy(str,2,pos(',',str)-2);
str:=copy(str,pos(',',str)+1,length(str)-pos(',',str));
依次类推
 
使用StrPos找到逗号,用AnsiQuotedStr去掉引号。
 
str:string;
str:="13004642216","jine@cnuninet.com","金拥政",780.00;
Edit1.text:=copy(str,2,pos(',',str)-2);
str:=copy(str,pos(',',str)+1,length(str)-pos(',',str));
依次类推

 
可以分行操作!txt文件:
13004642216
jine@cnuninet.com
金拥政
780.00
procedure button1click();
var
f:system.textfile;
s:string;
wj:string;
begin
opendialog1.filter:='文本文件(*.txt;*.*)|*.txt;*.*';
try
if opendialog1.execute then
begin
wj:=opendialog1.filename;
end;
setlength(s,1000);
assignfile(f,wj);
reset(f);
while not seekeof(f) do
begin
if seekeoln(f) then
readln;
read(f,s);
x:=x+1;
edit1.text:=s;
if x=1 then
edit2.text:=s;
if x=3 then
edit3.text:=s;
if x=4 then
edit4.text:=s;
end;
closefile(f);
except
end;
注意:x:integer;作为全局变量!
x在formcreate()中:
begin
x:=0;
end;
 
说清楚你的意图,还在字符串在文本文件里存放的格式。是一行一个呢,还是几个一行?
 
最简单的方法:
var
sl : TStringList;
i : integer;
Edit : TEdit;
begin
sl := TStringList.Create;
sl.Text := '"13004642216","jine@cnuninet.com","金拥政",780.00';
sl.Text := StringReplace(sl.Text, ',', #$D#$A, [rfReplaceAll]);
sl.Text := Trim(StringReplace(sl.Text, '"', '', [rfReplaceAll]));
for i:= 0 to sl.Count-1 do
begin
Edit := FindComponent('Edit'+IntToStr(i+1)) as TEdit;
Edit.Text := sl.Strings;
end;
end;
通过测试。
 
str:string;
str:='''3004642216'',''jine@cnuninet.com'',''金拥政'',780.00';
Edit1.text:=MP_ReplaceStr(copy(str,1,pos(',',str)-1),'''','',true,true);
str:=delete(str,1,pos(',',str);
edit2及以下类推.
使用函数如下:
function MP_ReplaceStr(ss,SubStr,NewStr:string;RepAll,IgCase:Boolean):string;
var
a:TReplaceFlags;
begin
if RepAll=true then
include(a,rfReplaceAll);
if IgCase=true then
include(a,rfIgnoreCase);
Result:=stringReplace(ss,Substr,Newstr,a);
end;
 
是有很多行,顺序读出,然后处理数据
 
procedure TForm1.Button1Click(Sender: TObject);
var
strList:TStringList;
i:integer;
begin
strList:=TStringList.Create;
strList.LoadFromFile('...');
for i :=0 to strList.Count-1 do
begin
ShowMessage(strList.Strings);
使用StrPos找到逗号,用AnsiQuotedStr去掉引号。


end;


end;
 
T0:教父:

"13004642216","jine@cnuninet.com","金拥政",780.00
"13004642216","jine@cnuninet.com","金拥政",780.00
"13004642216","jine@cnuninet.com","金拥政",780.00
"13004642216","jine@cnuninet.com","金拥政",780.00
要顺序的取出数据
var
sl,List: TStringList;
i,n : integer;
Edit : TEdit;
begin
List:=TStringList.Create;
List.LoadFromFile('c:/b/db.txt');
for n:=0 to List.count-1 do
begin
sl := TStringList.Create;
sl.Text := List.Strings[n];
sl.Text := StringReplace(sl.Text, ',', #$D#$A, [rfReplaceAll]);
sl.Text := Trim(StringReplace(sl.Text, '"', '', [rfReplaceAll]));
for i:= 0 to sl.Count-1 do
begin
Edit := FindComponent('Edit'+IntToStr(i+1)) as TEdit;
Edit.Text := sl.Strings;
end;
end;
end;
 
try

var
sl,List: TStringList;
i,n : integer;
Edit : TEdit;
begin
List:=TStringList.Create;
List.LoadFromFile('c:/b/db.txt');
for n:=0 to List.count-1 do
begin
sl := TStringList.Create;
sl.Text := List.Strings[n];
sl.Text := StringReplace(sl.Text, ',', #$D#$A, [rfReplaceAll]);
sl.Text := Trim(StringReplace(sl.Text, '"', '', [rfReplaceAll]));
//add
sl.CommaText := Trim(StringReplace(sl.Text, ' ', ',', [rfReplaceAll]));

for i:= 0 to sl.Count-1 do
begin
Edit := FindComponent('Edit'+IntToStr(i+1)) as TEdit;
Edit.Text := sl.Strings;
end;
end;
end;
 
我一下的处理某个程序时用的程序是用
TempMsgStr:=TempTxtFile.Lines[j];//文件的某一行为[13004642216][jine@cnuninet.com][金拥政][780.00]
for i := 0 to 3 do
begin
UserStart:=Pos('[',TempMsgStr);
UserEnd:=Pos(']',TempMsgStr);
MsgInfo:=Copy(TempMsgStr,UserStart+1,UserEnd-UserStart-1);//获得接收者
TempMsgStr:=Copy(TempMsgStr,UserEnd+1,length(TempMsgStr)-UserEnd);
end;
处理后:
MSGInfo[0]:=13004642216;
MSGInfo[1]:=jine@cnuninet.com;
MSGInfo[2]:=金拥政;
MSGInfo[3]:=780.00;
 
后退
顶部