|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I'm trying to create an examprog, i want to put the questions in a notepad, how can i make my program read the question from the text file or notepad and at the same time generate a text file when i use the program?
|
|
#2
|
||||
|
||||
|
Code:
Dim iReadFile As Integer Dim iWriteFile As Integer iReadFile = FreeFile 'set to next avail file pointer # iWriteFile = FreeFile 'set to next avail file pointer # Open "your input file" For Input As #iReadFile Open "your output file" For Output As #iWriteFile 'etc... |
|
#3
|
|||
|
|||
|
what is pointer # and can u show me a demo?
|
|
#4
|
|||
|
|||
|
you can find freefile on the msdn.Freefile return a file pointer which is never used before.The file pointer is used to write data fto file or read data from file.
Sample: Dim iReadFile As Integer Dim iWriteFile As Integer dim Strvar as string iReadFile = FreeFile 'set to next avail file pointer # iWriteFile = FreeFile 'set to next avail file pointer # 'open files Open "your input file" For Input As #iReadFile Open "your output file" For Output As #iWriteFile 'write file print #iWriteFile, "This is a sample." 'read file input #iReadFile,Strvar 'close file Close #iWriteFile Close #iReadFile ... But U can't write & read a file as same time!. |
|
#5
|
|||
|
|||
|
I've modified you're code...
Dim iReadFile As Integer Dim iWriteFile As Integer dim Strvar as string iReadFile = FreeFile 'set to next avail file pointer # iWriteFile = FreeFile 'set to next avail file pointer # 'open files Open "your input file" For Input As #iReadFile Open "your output file" For Output As #iWriteFile 'write file print #iWriteFile, "This is a sample." 'read file input #iReadFile,Strvar 'close file Close #iWriteFile Close #iReadFile with this... Dim MyIndex, FileNumber For MyIndex = 1 To 5 FileNumber = FreeFile Open "TEST" & MyIndex & ".TXT" For Output As #FileNumber Write #FileNumber, "This is a sample." Close #FileNumber Next MyIndex though it's almost the same as your codes only shorter, and besides when i tried youre code an error occurs on "Open "your input file" For Input As #iReadFile" i tried to put your code into a command button so when i click it that'll be the time it will generate a file. Where did you use the For Input As? and what does this '#FileNumber' represent? thnx in advance |
|
#6
|
||||
|
||||
|
Here is an example:
Create a form, put a command button on it and use this code for the command button Code:
Private Sub Command1_Click()
Dim iWriteFile, iReadFile As Integer
Dim sLine As String
iWriteFile = FreeFile
Open App.Path & "/test.txt" For Output As #iWriteFile
Print #iWriteFile, App.hInstance
Close #iWriteFile
iReadFile = FreeFile
Open App.Path & "/test.txt" For Input As #iReadFile
While Not EOF(iReadFile)
Line Input #iReadFile, sLine
MsgBox "This was recieved by the file: " & sLine, vbOKOnly
Wend
Close #iReadFile
End Sub
|
|
#7
|
|||
|
|||
|
now that's what i'm talking about, thanks onslaught!
Just one more thing, How can i delete the text file i've created in run time? |
|
#8
|
||||
|
||||
|
look in the msdn library for the filesystemobject (fso)
|
|
#9
|
|||
|
|||
|
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True) a.WriteLine("This is a test.") a.Close I've tried looking at the FileSystemObject in msdn and found the above codes. The above codes are another way of creating a text file. I've tried to search about the DeleteFile method but i found only syntaxes and I'm having errors and a hard time where to put the DeleteFile. Can u please tell me whee I should put the DeleteFile. thnx |
|
#10
|
||||
|
||||
|
The following is added to the above sample form that I posted above. I created two other command buttons and added the following code:
Code:
Private Sub Command2_Click()
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.DeleteFile (App.Path & "/test.txt")
MsgBox "File was deleted by method: FSO.DeleteFile(filename)"
Set FSO = Nothing
End Sub
Private Sub Command3_Click()
Dim FSO As Object
Dim File As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set File = FSO.GetFile(App.Path & "/test.txt")
File.Delete
MsgBox "File was deleted by method: FileObject.Delete"
Set File = Nothing
Set FSO = Nothing
End Sub
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > reading and generating a text file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|