以下是一段 《PB编程百例通》 里的一个“轻松背单词” 的源码,有些不懂,请教

snowmanliang 2004-04-07 01:08:51
原理是 通过一些文件操作函数 打开,并用流模式 读文件(流模式是什么啊?)

主窗口有 st_1,st_2 显示单词和意义
在事先写单词的 word.txt里每个单词占一行,然后单词与词义用TAB分开,

在窗口的OPEN事件中:
integer li_FileNum//打开文件的句柄
integer li_loops//读取文件的次数
integer li_i
long ll_flen, ll_bytes_read
string ls_part
SetPointer(HourGlass!)
this.setposition( topmost!)
ll_flen = FileLength("word.txt")//得到文件的长度
li_FileNum = FileOpen("word.txt", StreamMode!, Read!, LockRead!)
//以读的模式打开文件,按字节读取
//因为每次读文件最多只能读取32765个字节,所以应计算读取次数
IF ll_flen > 32765 THEN
IF Mod(ll_flen, 32765) = 0 THEN
li_loops = ll_flen//能整除
ELSE//不能整除
li_loops = (ll_flen/32765) + 1
END IF
ELSE
li_loops = 1
END IF
FOR li_i = 1 to li_loops//把整个文件组合成一个字符串
ll_bytes_read = FileRead(li_FileNum, ls_part)
is_total = is_total + ls_part
NEXT
FileClose(li_FileNum)
il_pre=1
timer(2)

timer事件中:
long ll_now, ll_pos
string ls_current
ll_now=pos(is_total,char(13),il_pre)
//在整个字符串中查找回车字符的位置
ls_current=mid(is_total,il_pre,ll_now - il_pre+1)
//得到一行数据
il_pre=ll_now+2
ll_pos = pos(ls_current,char(9),1)
//找到tab字符的位置
st_1.text=mid(ls_current,1,ll_pos - 1)
st_2.text=mid(ls_current,ll_pos + 1 )

原理没完全明白,可否指点一下?
this.width = 45*max(len(st_1.text),len(st_2.text))
...全文
63 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
snowmanliang 2004-04-07
  • 打赏
  • 举报
回复
那为什么要乘45呢??要是文本的长度乘2不就够了吗?长度指??
还有,按照师兄说的,我觉得这个例子用行模式更好了?
另外 ,fileopen,fileread,不大明白,它open指open在哪呢,用户看不到的吧?只是取里面的内容啊,:)
thelazyman 2004-04-07
  • 打赏
  • 举报
回复
"this.width = 45*max(len(st_1.text),len(st_2.text))"
确定窗体的宽度,取两个文本框中最长的长度乘以45
这样确保文本都能显示完全.
klbt 2004-04-07
  • 打赏
  • 举报
回复
freedom2001(天翔) 的话很有深意......
freedom2001 2004-04-07
  • 打赏
  • 举报
回复
晕,注释的这么详细了,还说不明白,这里面好象没牵扯到什么原理啊,你还是好好看看书吧,不要老是看这些技巧的东西,基本知识没掌握好,学那些技巧都没用,你总不能以后工作的时候,拿本书招着往上抄代码吧
this.width = 45*max(len(st_1.text),len(st_2.text))
这句的意思就是宽度=45*(st_1.text和st_2.text中长度最大的那个字符串的长度)
max总学过是什么意思吧
balloonman2002 2004-04-07
  • 打赏
  • 举报
回复
PB中文件操作模式有两种:LineMode和StreamMode,分别称为行模式和流模式:
When a file has been opened in line mode, each call to the FileRead function reads until it encounters a carriage return (CR), linefeed (LF), or end-of-file mark (EOF). Each call to FileWrite adds a CR and LF at the end of each string it writes.
When a file has been opened in stream mode, a call to FileRead reads the whole file (until it encounters an EOF) or 32,765 bytes, whichever is less. FileWrite writes a maximum of 32,765 bytes in a single call and does not add CR and LF characters.
换言之:行模式每次读一行数据,这一行怎么来届定?就是看是否碰到回车、换行符,因此这种模式读取速度较慢,但是易拆分读出的数据;
流模式是每次读取32765字节数据,读取速度快,但不易拆分结果数据;

1,079

社区成员

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

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