|
-
Feb 12th, 2012, 09:23 PM
#1
Thread Starter
New Member
[RESOLVED] [HELP] Read From Text File
Hey Guys!
I need your help, again. Two guys did an awesome job helping me out, and I really like this community so far.
So my question is:
I want to create this code:
When my program starts, I want it to create a folder called "vMod" and make data.ini/data.dat/data.txt in it, but ONLY if it doesn't exist. (I prefer data.ini).
When it's created, I want this .v. text to save in it, again only if it doesn't exist.
Then, for example, I have a text box and I want to read the "Name line" and put "vMod" or whatever string in the txtText.Text.
After that, I want "Kills Line" string in txtText1.Text. I just want 0 or whatever number to be in txtText1.Text, not the whole "Kills 0", just "0". And I will add 1 more line by my self, later. Thanx
Also, please comment on the code explaining it, I am 14 years old and I want programming to be my job when I grow up. Thanks In Advanced!
-
Feb 13th, 2012, 12:16 AM
#2
Re: [HELP] Read From Text File
Start new project, add Text1 and Text2 to the Form and use the following code.
vb Code:
Private Const mstrDataFolder As String = "g:\vMode" ' Replace with your path Private Const mstrDataFile As String = "g:\vMode\data.ini" ' Replace with your file name Private Sub Form_Load() CreateDataFolder CreateDataFile ReadDataFile End Sub Private Sub CreateDataFolder() ' Check if the folder exist, if it isn't then create it. If Dir(mstrDataFolder, vbDirectory) = vbNullString Then MkDir mstrDataFolder End If End Sub Private Sub CreateDataFile() ' Check if the file exist, if it isn't then create and initialize it. If Dir(mstrDataFile) = vbNullString Then Dim lngFileNumber As Long lngFileNumber = FreeFile Open mstrDataFile For Output As #lngFileNumber Print #lngFileNumber, "Name vMod" Print #lngFileNumber, "Kills 0" Close #lngFileNumber End If End Sub Private Sub ReadDataFile() Dim lngFileNumber As Long Dim strName As String Dim strKills As String ' read Name and Kills from the file lngFileNumber = FreeFile Open mstrDataFile For Input As #lngFileNumber Line Input #lngFileNumber, strName Line Input #lngFileNumber, strKills Close #lngFileNumber ' Show Name and Kills in Text1 and Text2 Text1.Text = Mid$(strName, InStr(1, strName, " ") + 1) Text2.Text = Mid$(strKills, InStr(1, strKills, " ") + 1) End Sub
-
Feb 13th, 2012, 06:21 AM
#3
Thread Starter
New Member
Re: [HELP] Read From Text File
 Originally Posted by 4x2y
Start new project, add Text1 and Text2 to the Form and use the following code.
vb Code:
Private Const mstrDataFolder As String = "g:\vMode" ' Replace with your path
Private Const mstrDataFile As String = "g:\vMode\data.ini" ' Replace with your file name
Private Sub Form_Load()
CreateDataFolder
CreateDataFile
ReadDataFile
End Sub
Private Sub CreateDataFolder()
' Check if the folder exist, if it isn't then create it.
If Dir(mstrDataFolder, vbDirectory) = vbNullString Then
MkDir mstrDataFolder
End If
End Sub
Private Sub CreateDataFile()
' Check if the file exist, if it isn't then create and initialize it.
If Dir(mstrDataFile) = vbNullString Then
Dim lngFileNumber As Long
lngFileNumber = FreeFile
Open mstrDataFile For Output As #lngFileNumber
Print #lngFileNumber, "Name vMod"
Print #lngFileNumber, "Kills 0"
Close #lngFileNumber
End If
End Sub
Private Sub ReadDataFile()
Dim lngFileNumber As Long
Dim strName As String
Dim strKills As String
' read Name and Kills from the file
lngFileNumber = FreeFile
Open mstrDataFile For Input As #lngFileNumber
Line Input #lngFileNumber, strName
Line Input #lngFileNumber, strKills
Close #lngFileNumber
' Show Name and Kills in Text1 and Text2
Text1.Text = Mid$(strName, InStr(1, strName, " ") + 1)
Text2.Text = Mid$(strKills, InStr(1, strKills, " ") + 1)
End Sub
WoW Thanks, that was pretty good, +REP Definitely. The only other thing I need, is so it loads from App.Path. I have tried it and it gave me errors. So can you please fix it? Thanks.
-
Feb 13th, 2012, 06:48 AM
#4
Re: [HELP] Read From Text File
 Originally Posted by Vlad1k
The only other thing I need, is so it loads from App.Path. I have tried it and it gave me errors.
I have declared paths as constant for demonstration only, to fix, replace declaration and Form_Load with
vb Code:
Private mstrDataFolder As String Private mstrDataFile As String Private Sub Form_Load() If Right$(App.Path, 1) = "\" Then ' it is drive root, so don't append slash mstrDataFolder = App.Path & "vMode" Else mstrDataFolder = App.Path & "\vMode" End If mstrDataFile = mstrDataFolder & "\data.ini" CreateDataFolder CreateDataFile ReadDataFile End Sub
-
Feb 13th, 2012, 08:05 AM
#5
Thread Starter
New Member
Re: [HELP] Read From Text File
Cool +REP Again, but I need some more help. :-/ I want to save file data.ini, but I only want to save Kills Line, not the name line, the name line I want to stay the same as it was. Thnx Again. Really good job
-
Feb 13th, 2012, 08:22 AM
#6
Re: [HELP] Read From Text File
Vlad1k,
if you just started to learn programming why do you use very outdated programming language?
There is much more robust and far more advanced language available - VB 2010 Express. It's free and you can get it directly from Microsoft.
-
Feb 13th, 2012, 08:23 AM
#7
Thread Starter
New Member
Re: [HELP] Read From Text File
Well, my step-dad has a EnterPrise edition of VB6 that he got from his college on his birthday. I don't want to switch because I have a old computer, and I am fine with what I have.
-
Feb 13th, 2012, 08:44 AM
#8
Re: [HELP] Read From Text File
Of course it's up to you but since you're only 14 do it now before it's too late.
Those days when you can learn one thing and go with it for awhile are gone.
Good luck.
-
Feb 13th, 2012, 09:02 AM
#9
Thread Starter
New Member
Re: [HELP] Read From Text File
I will download 2010 Edition when I will get a new laptop, for now I am creating this awesome Server Mod, and it might be incompatible with 2010.
Can anyone say how I can save this file without changing the Name ***? I just want to save it as Kills ****
-
Feb 13th, 2012, 09:28 AM
#10
Re: [HELP] Read From Text File
 Originally Posted by Vlad1k
I will download 2010 Edition when I will get a new laptop, for now I am creating this awesome Server Mod, and it might be incompatible with 2010.
Can anyone say how I can save this file without changing the Name ***? I just want to save it as Kills ****
You got code to check/create folder and file and you got code to read and display the file. If the file is initially created it is filled with initial data as you told 4x2y.
If you changed your mind now, read the code, find the line that does the initial writting of the name and delete it or mark this line as a comment ('). You should be able to do at least that on your own! We do llike to get Rep-Points, but what we do like even more, is seeing the a comment has been understood and OP is realy using new coding-skills (which are paste©!).
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Feb 13th, 2012, 11:01 AM
#11
Thread Starter
New Member
Re: [HELP] Read From Text File
All right, I think I do know what you talking about. It was 5 am when I posted this. I will do this. Thanks.
-
Feb 13th, 2012, 02:43 PM
#12
Frenzied Member
Re: [HELP] Read From Text File
i think rhino has a point if you are only 14 and you want this to be your job, you will probably want to try a university or college course of a higher degree like some of the rest of us have done and you do not want to be learning something that changes so fast because all of the problem solving skills and concepts you may or maynot have learned will be a complete waste if you are using the first programming language that comes along ( to you ) you had better write a calculation program and work out when you will be applying for your first real programming job and then apply for vb20xx replace xx with the year of atleast the first if not the 2nd year of employment and then get the books out for microsoft 2100 as it may take a while for you the catch up with all of the hints and tricks that your old programming language could have taught you... yes i know there is no punctuation here.. its a rant!
The reall important thing to learn in programming is that there is no such thing as the best language or even the right language for a given problem, but some languages are better suited than others.
I personally have written in too many languages to list here, each one has merit and each one teaches you something about programming and the nature of programatically solvable problems..
do not be put off, try them all especially the fre ones and the up and coming ones like "D" and "E" try not to be bogged down by these attitudes.
keep programming and learning
learn concepts not rote!
</rant>
-
Feb 15th, 2012, 07:55 AM
#13
Thread Starter
New Member
Re: [HELP] Read From Text File
I could not do it. Can some one help me please?
-
Feb 15th, 2012, 08:00 AM
#14
Re: [HELP] Read From Text File
You could not do what? What have you done so far? This is not chit-chat and you have to show us your effort by at least showing your actual code.
-
Feb 15th, 2012, 08:07 AM
#15
Re: [HELP] Read From Text File
 Originally Posted by Vlad1k
I could not do it. Can some one help me please?
You already read Name and stored its value in Text1, Kills and stored in Text2, so all you need is write them again
vb Code:
Private Sub SaveDataFile()
Dim lngFileNumber As Long
lngFileNumber = FreeFile
Open mstrDataFile For Output As #lngFileNumber
Print #lngFileNumber, "Name " & Text1.Text
Print #lngFileNumber, "Kills " & Text2.Text
Close #lngFileNumber
End Sub
BTW: if the user isn't allowed to change the Name, set the Text1.Locked = True
-
Feb 15th, 2012, 09:10 AM
#16
Thread Starter
New Member
Re: [HELP] Read From Text File
I got it to work, because I renamed the file to kills.ini, because I didn't need the first line (Name vMod), and instead of having Kills 0 in that file, I just put 0. Very efficient. I also put a check to check for a file and folder. Thanx guys.
Resolved.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|