Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 14th, 2003, 04:19 PM
crackerweb crackerweb is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2003
Posts: 787 crackerweb User rank is Private First Class (20 - 50 Reputation Level)crackerweb User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 18 h 18 m 15 sec
Reputation Power: 6
Question reading and generating a text file

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?

Reply With Quote
  #2  
Old October 14th, 2003, 10:41 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,840 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 36 m 16 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
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...

Reply With Quote
  #3  
Old October 15th, 2003, 03:20 PM
crackerweb crackerweb is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2003
Posts: 787 crackerweb User rank is Private First Class (20 - 50 Reputation Level)crackerweb User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 18 h 18 m 15 sec
Reputation Power: 6
what is pointer # and can u show me a demo?

Reply With Quote
  #4  
Old October 15th, 2003, 11:33 PM
cleverpig cleverpig is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2003
Posts: 1,152 cleverpig User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via MSN to cleverpig
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!.

Reply With Quote
  #5  
Old October 16th, 2003, 07:37 PM
crackerweb crackerweb is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2003
Posts: 787 crackerweb User rank is Private First Class (20 - 50 Reputation Level)crackerweb User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 18 h 18 m 15 sec
Reputation Power: 6
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

Reply With Quote
  #6  
Old October 16th, 2003, 09:34 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,840 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 36 m 16 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
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

Reply With Quote
  #7  
Old October 17th, 2003, 05:41 PM
crackerweb crackerweb is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2003
Posts: 787 crackerweb User rank is Private First Class (20 - 50 Reputation Level)crackerweb User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 18 h 18 m 15 sec
Reputation Power: 6
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?

Reply With Quote
  #8  
Old October 17th, 2003, 10:17 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,840 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 36 m 16 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
look in the msdn library for the filesystemobject (fso)

Reply With Quote
  #9  
Old October 18th, 2003, 03:52 PM
crackerweb crackerweb is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2003
Posts: 787 crackerweb User rank is Private First Class (20 - 50 Reputation Level)crackerweb User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 18 h 18 m 15 sec
Reputation Power: 6
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

Reply With Quote
  #10  
Old October 18th, 2003, 10:16 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,840 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 36 m 16 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
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
This, of course, does not check if the file exists.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > reading and generating a text file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT