VB中如何获得txt文本文件中指定某行的内容.....

vbhh 2000-02-21 11:31:00
Hi:
请教一个问题:
VB中如何获得txt文本文件中指定某行的内容.....
如:1.txt文件中有如下内容:

1、The first line content;
2、The second line content;
3、The third line content;
4、The fourth line content;

问:如何获得第三行的内容,即 "3、The third line content;"请指点一二......

vbhh
...全文
2355 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
benbenlittle 2000-02-26
  • 打赏
  • 举报
回复
我的基本思路和dendun一样
不过如果这个txt文件被读到textbox里可以用api函数取得
SendMessage text.hwnd,EM_GETLINE,行数,ByVal S
 其中s为字符串变量通过这个函数s为这行的内容
 s的长度可用
POS=SendMessage text.hwnd,EM_LINEINDEX,行数,ByVal 0&
LENGTH=SendMessage text.hwnd,EM_LINELENGTH,POS,ByVal 0&
 求得
 
upstream 2000-02-21
  • 打赏
  • 举报
回复
读文件,数回车,适应任何语言.
active 2000-02-21
  • 打赏
  • 举报
回复
可以使用VB6中的FileSystemObject 对象和TextStream 对象
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)
a.skipline
?a.readline
a.Close
Firing_Sky 2000-02-21
  • 打赏
  • 举报
回复
另外顺便一提的是,如果你用的是VB6的话,不妨使用文件系统对象(File System Objects),它比传统的方式更加高效
用法如下:

Dim fso as new filesystemobjects, ts as new TextStream
txtfile,fill as File
Set txtfile =fso.CreateTextFile("c:\1.txt", true)
Set fill=fso.GetFile("c:\1.txt")
Set ts=fill.OpenAsStream(forWriting)
for i=1 to 4
ts.Write "Firing_Sky"
next
ts.close
Set ts=fill.OpenAsStream(forReading)
for i=1 to 4
s=ts.ReadLine
next
ts.close
Firing_Sky 2000-02-21
  • 打赏
  • 举报
回复
我觉得只能以行一行的读取,就像 dengdun 给出的源代码一样
因为txt文件是一个顺序文件,不可能立即找到指定的位置的
dengdun 2000-02-21
  • 打赏
  • 举报
回复
Dim strTemp As String
Dim iLine As Integer
Open "testFile.dat" For Input As #1 '打开文件
iLine = 1
Do While Not EOF(1) And iLine < 3 '循环读取一行数据
Line Input #1, strTemp '读入一行数据
iLine = iLine + 1
Loop
Close #1 '关闭文件
Un1 2000-02-21
  • 打赏
  • 举报
回复
dim sContent as String
sContent = String(1024, 0)
SendMessage TextBox.hWnd, EM_GETLINE, lLine, sContent

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧