| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 473 人关注过本帖
标题:[求助]读取文件
只看楼主 加入收藏
桃源书生
Rank: 1
等 级:新手上路
帖 子:99
专家分:3
注 册:2006-7-3
结帖率:33.33%
收藏
 问题点数:0 回复次数:4 
[求助]读取文件

通过commondialog控件读入文件,代码如下:

CommonDialog1.Filter = "文本文件(*.txt)|*.txt"
On Error GoTo CloseError
CommonDialog1.ShowSave
If Dir(CommonDialog1.FileName) <> "" Then
If MsgBox("文件" & CommonDialog1.FileName & "已存在," & vbCrLf & "要替换它吗?", vbYesNo + vbQuestion + vbDefaultButton2) = vbNo Then Exit Sub
End If

我想把所读入的文件*.txt的数据赋值给一个数组a,但是该文件名是不确定的(即由用户自己定义的),请问实现呢?谢谢!

搜索更多相关主题的帖子: 文件 
2006-12-18 10:18
bestfeng
Rank: 3Rank: 3
等 级:新手上路
威 望:7
帖 子:179
专家分:0
注 册:2006-10-31
收藏
得分:0 
commondialog1.showopen
dim Files as string
files = commondialog1.filename
然后读入。这个问题书上基本上都有。你去看好了
2006-12-18 10:22
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
CommonDialog控件...只能让你选择一个文件...至于具体的读入...要你自己去写代码实现..

我的msn: myfend@
2006-12-18 10:27
学习VB才2天
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:1653
专家分:0
注 册:2006-5-4
收藏
得分:0 
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
在窗体上放置文本框和命令按钮各一个,并粘帖如下代码,启动程序按下按钮即会打开选择文件对话框。
Private Sub FileOpen_Click()
Dim ofn As OPENFILENAME
Dim rtn As String
ofn.lStructSize = Len(ofn)
ofn.hwndOwner = Me.hWnd
ofn.hInstance = App.hInstance
ofn.lpstrFilter = "所有文件"
ofn.lpstrFile = Space(254)
ofn.nMaxFile = 255
ofn.lpstrFileTitle = Space(254)
ofn.nMaxFileTitle = 255
ofn.lpstrInitialDir = App.Path
ofn.lpstrTitle = "打开文件"
ofn.flags = 6148
rtn = GetOpenFileName(ofn)
If rtn >= 1 Then
Filename.Text = ofn.lpstrFile
Else
Filename.Text = "Cancel Was Pressed"
End If
End SuPrivate Sub FileOpen_Click()
Dim ofn As OPENFILENAME
Dim rtn As String
ofn.lStructSize = Len(ofn)
ofn.hwndOwner = Me.hWnd
ofn.hInstance = App.hInstance
ofn.lpstrFilter = "所有文件"
ofn.lpstrFile = Space(254)
ofn.nMaxFile = 255
ofn.lpstrFileTitle = Space(254)
ofn.nMaxFileTitle = 255
ofn.lpstrInitialDir = App.Path
ofn.lpstrTitle = "打开文件"
ofn.flags = 6148
rtn = GetOpenFileName(ofn)
If rtn >= 1 Then
Filename.Text = ofn.lpstrFile
Else
Filename.Text = "Cancel Was Pressed"
End If
End Sub------------------------------------------------------

[GLOW=255,DeepPink,3]我的免费网盘[/GLOW]
2006-12-18 15:28
donghua200
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2006-9-4
收藏
得分:0 
2007-05-16 14:04
快速回复:[求助]读取文件
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.093482 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved