Results 1 to 16 of 16

Thread: [RESOLVED] [HELP] Read From Text File

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    St. Louis
    Posts
    14

    Resolved [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.

    Code:
    Name vMod
    Kills 0
    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!

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: [HELP] Read From Text File

    Start new project, add Text1 and Text2 to the Form and use the following code.

    vb Code:
    1. Private Const mstrDataFolder As String = "g:\vMode" ' Replace with your path
    2. Private Const mstrDataFile As String = "g:\vMode\data.ini" ' Replace with your file name
    3.  
    4. Private Sub Form_Load()
    5.     CreateDataFolder
    6.     CreateDataFile
    7.     ReadDataFile
    8. End Sub
    9.  
    10. Private Sub CreateDataFolder()
    11.     ' Check if the folder exist, if it isn't then create it.
    12.     If Dir(mstrDataFolder, vbDirectory) = vbNullString Then
    13.         MkDir mstrDataFolder
    14.     End If
    15. End Sub
    16.  
    17. Private Sub CreateDataFile()
    18.     ' Check if the file exist, if it isn't then create and initialize it.
    19.     If Dir(mstrDataFile) = vbNullString Then
    20.         Dim lngFileNumber As Long
    21.        
    22.         lngFileNumber = FreeFile
    23.         Open mstrDataFile For Output As #lngFileNumber
    24.         Print #lngFileNumber, "Name vMod"
    25.         Print #lngFileNumber, "Kills 0"
    26.         Close #lngFileNumber
    27.     End If
    28. End Sub
    29.  
    30. Private Sub ReadDataFile()
    31.     Dim lngFileNumber As Long
    32.     Dim strName As String
    33.     Dim strKills As String
    34.    
    35.     ' read Name and Kills from the file
    36.     lngFileNumber = FreeFile
    37.     Open mstrDataFile For Input As #lngFileNumber
    38.     Line Input #lngFileNumber, strName
    39.     Line Input #lngFileNumber, strKills
    40.     Close #lngFileNumber
    41.    
    42.     ' Show Name and Kills in Text1 and Text2
    43.     Text1.Text = Mid$(strName, InStr(1, strName, " ") + 1)
    44.     Text2.Text = Mid$(strKills, InStr(1, strKills, " ") + 1)
    45.    
    46. End Sub



  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    St. Louis
    Posts
    14

    Thumbs up Re: [HELP] Read From Text File

    Quote Originally Posted by 4x2y View Post
    Start new project, add Text1 and Text2 to the Form and use the following code.

    vb Code:
    1. Private Const mstrDataFolder As String = "g:\vMode" ' Replace with your path
    2. Private Const mstrDataFile As String = "g:\vMode\data.ini" ' Replace with your file name
    3.  
    4. Private Sub Form_Load()
    5.     CreateDataFolder
    6.     CreateDataFile
    7.     ReadDataFile
    8. End Sub
    9.  
    10. Private Sub CreateDataFolder()
    11.     ' Check if the folder exist, if it isn't then create it.
    12.     If Dir(mstrDataFolder, vbDirectory) = vbNullString Then
    13.         MkDir mstrDataFolder
    14.     End If
    15. End Sub
    16.  
    17. Private Sub CreateDataFile()
    18.     ' Check if the file exist, if it isn't then create and initialize it.
    19.     If Dir(mstrDataFile) = vbNullString Then
    20.         Dim lngFileNumber As Long
    21.        
    22.         lngFileNumber = FreeFile
    23.         Open mstrDataFile For Output As #lngFileNumber
    24.         Print #lngFileNumber, "Name vMod"
    25.         Print #lngFileNumber, "Kills 0"
    26.         Close #lngFileNumber
    27.     End If
    28. End Sub
    29.  
    30. Private Sub ReadDataFile()
    31.     Dim lngFileNumber As Long
    32.     Dim strName As String
    33.     Dim strKills As String
    34.    
    35.     ' read Name and Kills from the file
    36.     lngFileNumber = FreeFile
    37.     Open mstrDataFile For Input As #lngFileNumber
    38.     Line Input #lngFileNumber, strName
    39.     Line Input #lngFileNumber, strKills
    40.     Close #lngFileNumber
    41.    
    42.     ' Show Name and Kills in Text1 and Text2
    43.     Text1.Text = Mid$(strName, InStr(1, strName, " ") + 1)
    44.     Text2.Text = Mid$(strKills, InStr(1, strKills, " ") + 1)
    45.    
    46. 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.

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: [HELP] Read From Text File

    Quote Originally Posted by Vlad1k View Post
    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:
    1. Private mstrDataFolder As String
    2. Private mstrDataFile As String
    3.  
    4. Private Sub Form_Load()
    5.    
    6.     If Right$(App.Path, 1) = "\" Then ' it is drive root, so don't append slash
    7.         mstrDataFolder = App.Path & "vMode"
    8.     Else
    9.         mstrDataFolder = App.Path & "\vMode"
    10.     End If
    11.  
    12.     mstrDataFile = mstrDataFolder & "\data.ini"
    13.    
    14.     CreateDataFolder
    15.     CreateDataFile
    16.     ReadDataFile
    17. End Sub



  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    St. Louis
    Posts
    14

    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

  6. #6

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    St. Louis
    Posts
    14

    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.

  8. #8

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    St. Louis
    Posts
    14

    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 ****

  10. #10
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [HELP] Read From Text File

    Quote Originally Posted by Vlad1k View Post
    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&copy!).
    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!

  11. #11

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    St. Louis
    Posts
    14

    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.

  12. #12
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    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>

  13. #13

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    St. Louis
    Posts
    14

    Re: [HELP] Read From Text File

    I could not do it. Can some one help me please?

  14. #14

  15. #15
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: [HELP] Read From Text File

    Quote Originally Posted by Vlad1k View Post
    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:
    1. Private Sub SaveDataFile()
    2.     Dim lngFileNumber As Long
    3.    
    4.     lngFileNumber = FreeFile
    5.     Open mstrDataFile For Output As #lngFileNumber
    6.     Print #lngFileNumber, "Name " & Text1.Text
    7.     Print #lngFileNumber, "Kills " & Text2.Text
    8.     Close #lngFileNumber
    9. End Sub

    BTW: if the user isn't allowed to change the Name, set the Text1.Locked = True



  16. #16

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    St. Louis
    Posts
    14

    Resolved 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
  •  



Click Here to Expand Forum to Full Width