遇到一个怪问题,关于读取ini 文件的问题

hengxin54 2002-07-13 12:39:16
为什么从ini文件里读出的数据,如果是英文或数字,则一切显示正常,但如果读取的是中文,那么要显示的时候,如果在其后加一个字符串显示,则后面的字符串不能显示出来
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
private command1_click()
dim str as string * 256
getprivateprofilestring "system","version","",str,len(str),app.path & "\ini.ini"
msgbox str & "abcd"
end sub
如果读取的字符串是中文,则后面的“abcd"不能显示出来,但其它的能显示正常

...全文
64 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Small_C 2002-07-13
  • 打赏
  • 举报
回复
这是因为API函数中的字符串是用“Chr(0)”结束的。
上边少了“API”。我发早了对不起!
Small_C 2002-07-13
  • 打赏
  • 举报
回复
这是因为函数中的字符串是用“Chr(0)”结束的。
Chice_wxg 2002-07-13
  • 打赏
  • 举报
回复
private command1_click()
dim str as string
str = Space(256)
getprivateprofilestring "system","version","",str,len(str),app.path & "\ini.ini"
str=Trim(Left$(str,Instr(str,Chr$(0))-1))
msgbox str & "abcd"
end sub
Chice_wxg 2002-07-13
  • 打赏
  • 举报
回复
改成这样好了。

private command1_click()
dim str as string
str = Space(256)
getprivateprofilestring "system","version","",str,len(str),app.path & "\ini.ini"
str=Left$(str,Instr(str,Chr$(0))-1)
msgbox str & "abcd"
end sub

注意问题:
1、用dim .. as string * 256后系统自动分配内存,所以字符处理的时候可能有问题
2、注意字符穿的末尾的空字符(vbNullChar,就是Chr(0))
zyl910 2002-07-13
  • 打赏
  • 举报
回复
这样做当然不能显示
因为读取字符串是以\0结尾的(C语言字符串标准)
而MsgBox实际上是调用系统的MessageBox函数
碰到\0认为字符串结束
所以不会继续显示

应该这样写:
str=Left$(str,Instr(str,Chr$(0))-1) '查找\0
msgbox RTrim(str) & "abcd" '记得去掉定长字符串的多余空格
hengxin54 2002-07-13
  • 打赏
  • 举报
回复
多余的空字符去不掉,例如显示:“windows2000 abcd",中间的空字符去不掉,上面两种方法差不多的,都试了,不行

7,763

社区成员

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

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