|
|
导航: |
论坛 -> DELPHI技术
斑竹:liumazi,sephil |
|
作者: |
|
2018/11/11 11:28:06 |
标题: |
乱码文件里查找指定字符串的大难题。求大神~ |
浏览:1337 |
|
加入我的收藏 |
楼主: |
procedure TForm1.Button1Click(Sender: TObject); var f: TextFile; s: string; sPos, ePos: Integer; sL: TStringList; begin sL := TStringList.Create; AssignFile(f, 'a.log'); reset(f); while not eof(f) do begin Readln(f, s); // 赋值给S。 if Pos('TXN=', s) > 0 then begin // 找到指定字符串所在行 sPos := Pos('=', s); // 查找"="的位置 ePos := Pos(';', s); // 查找";"的位置 sL.Add(Copy(s, sPos + 1, ePos - sPos - 1)); // 含有";"就获取":"到";"的字符串 s1 := (sL.Text); sL.Clear; showmessage(s1); end; if Pos('TXP=', s) > 0 then begin // 找到指定字符串所在行 sPos := Pos('=', s); // 查找"="的位置 ePos := Pos(';', s); // 查找";"的位置 sL.Add(Copy(s, sPos + 1, ePos - sPos - 1)); // 含有";"就获取":"到";"的字符串 s2 := (sL.Text); sL.Clear; showmessage(s2); end; if Pos('"success":true,', s) > 0 then begin showmessage('找到了'); end; end; FreeAndNil(sL); closefile(f); end;
用该按键事件对无乱码的文件查找是正常的,如果文件中包含有乱码就不正常了,求大神指点,附上带乱码文件。。
----------------------------------------------
- |
作者: |
|
2018/11/11 11:38:18 |
1楼: |
charset=utf-8 TXP=wuwulili123;
----------------------------------------------
[alias] co = clone --recurse-submodules up = submodule update --init --recursiveupd = pullinfo = statusrest = reset --hard懒鬼提速https://www.cctry.com/>http://qalculate.github.io/downloads.htmlhttps://www.cctry.com/
|
作者: |
|
2018/11/11 11:42:57 |
2楼: |
charset=utf-8? 什么意思? 大哥,是要对文件进行编码吗?
----------------------------------------------
-
|
作者: |
|
2018/11/11 12:17:51 |
3楼: |
var f: TextFile; 这种方式只能处理纯文本文件,你所谓的乱码中遇到0或者一些非文本字符就会自动判断文件结束了。当然不行。
----------------------------------------------
武稀松http://www.raysoftware.cn
|
作者: |
|
2018/11/11 12:43:18 |
4楼: |
@武稀松 受教了,请问这种情况应该怎么处理? 要先把文件进行编码? 还是? 原因懂了,不过还不知道 怎么做。。我是菜B。。
----------------------------------------------
-
|
作者: |
|
2018/11/11 13:41:43 |
5楼: |
procedure TForm1.Button2Click(Sender: TObject); var SL : TStringList; i:string; begin SL := TStringList.Create; SL.LoadFromFile('a.log',TEncoding.UTF8); if SL.IndexOf('TXN=') > 0 then begin showmessage('ok'); end; SL.Free; end; 这样子试过了也不行,直接报错了。。
----------------------------------------------
-
|
作者: |
|
2018/11/11 15:17:19 |
6楼: |
点我上车
----------------------------------------------
[alias] co = clone --recurse-submodules up = submodule update --init --recursiveupd = pullinfo = statusrest = reset --hard懒鬼提速https://www.cctry.com/>http://qalculate.github.io/downloads.htmlhttps://www.cctry.com/
|
作者: |
|
2018/11/11 21:15:22 |
7楼: |
@ keymark 翻看了一些。都是乱码问题,和我的问题不同,我不用转换乱码,只是读里面的字符串,现在遇到情况是,有时行,有时不行。我晕。
----------------------------------------------
-
|
作者: |
|
2018/11/11 21:51:26 |
8楼: |
不知文件 从图上看 像被破坏(格式)了
规矩 http://bbs.2ccc.com/topic.asp?topicid=410924
此帖子包含附件:
大小:196.7K |
----------------------------------------------
[alias] co = clone --recurse-submodules up = submodule update --init --recursiveupd = pullinfo = statusrest = reset --hard懒鬼提速https://www.cctry.com/>http://qalculate.github.io/downloads.htmlhttps://www.cctry.com/
|
作者: |
|
2018/11/12 8:37:49 |
9楼: |
你试一试在内存流里面搜索
----------------------------------------------
delphi 是兴趣,和工作无关,即使它倒闭。又不靠它 delphi 吃饭,怕甚?
|
作者: |
|
2018/11/12 9:14:19 |
10楼: |
简单的说 txt 的内容已经被破坏了。 楼主 应该在 获取的源头,按 流 处理,不要按字符串处理。 delphi 的 string 是高级类型。他自带各种转换。 容易导致数据被破坏掉。
----------------------------------------------
(C)(P)Flying Wang
|
作者: |
|
2018/11/14 17:39:16 |
11楼: |
你读到流里面在内存里面搜索
----------------------------------------------
武稀松http://www.raysoftware.cn
|
|