CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    May 2001
    Posts
    4

    Ahhh... creating a text file...



    <newbieness mode>




    Hey everyone...
    How do I create a text file for a username and password to be stored in (its for a Maths program... doesn't need to be secure)

    Thanks,

    -Fubar


    </newbieness mode>




    I wanted to be a procrastinator, but never got round to it.

    <i> Remove NOSPAM_ to email me </i>

  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Ahhh... creating a text file...

    refer http://vblib.virtualave.net, there is a function that write text string array into text file called WriteFileText in vbFileIO as the activeX DLL.

    so, in order to store username and password, do this

    dim data(0) as string
    dim vbFileIO as new vbFileIO

    data(0) = "username" & " " & "password

    vbFileIO.WriteFileText(filename,data)...

    HTH


  3. #3
    Join Date
    May 2001
    Posts
    4

    Ok, Nearly there....


    Ok, before hacking me into tiny pieces (usually what happens to newbies) where do I put this zip file???

    Thanks again,

    -Fubar

    I wanted to be a procrastinator, but never got round to it.

    <i> Remove NOSPAM_ to email me </i>

  4. #4
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Ahhh... creating a text file...

    You can use this quick solution for storing your names and passwords in a textfile.

    Open App.Path & "pass.txt" for append as #1
    print #1, txtUser.Text
    print #1, txtPass.text
    Close #1





  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Ok, Nearly there....

    Here another way to do it. Put twotextboxes (first for name, second for password) and a commandbutton on a form. Copy and paste. (except the contents of mypass.txt: it is there only to provide an example).
    You may find .ini file or random file a better solution

    contents of mypass.txt file:

    gigi
    a
    bruno
    b
    pino
    c

    Option Explicit

    Private Sub Command1_Click()
    Dim freef As Integer
    freef = FreeFile
    Dim vntInput As Variant
    Dim blnFound As Boolean



    blnFound = False 'should be its default value
    'but initialize to it for clarity and security
    'serach for file of password
    If Dir("c:\mypass.txt") = "" Then
    'file does not exist. Create it
    Open "c:\mypass.txt" For Output As #freef
    Close #freef
    End If
    'in any case, now you have the file.
    'open it to search for username and password
    'do you want it case sensitive or not?
    '
    Open "c:\mypass.txt" For Input As #freef
    Do While EOF(freef) = False
    'first line the name
    Line Input #freef, vntInput
    'see if it matches with text1.text
    If Text1.Text = vntInput Then
    'look if password is all right
    Line Input #freef, vntInput
    If Text2.Text = vntInput Then
    'password ok
    MsgBox "You are a registered user"
    blnFound = True
    Exit Do
    Else
    MsgBox "You entered wrong password"
    blnFound = True
    Exit Do
    End If
    Else
    'name do not match, skip one line
    '(no need to lok for password)
    Line Input #freef, vntInput
    End If
    Loop
    Close #freef
    If blnFound = False Then
    MsgBox "User name not found"
    End If
    End Sub

    Private Sub Form_Load()
    Text2.PasswordChar = "*"
    End Sub


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Ok, Nearly there....

    ...as always, I was in a hurry, and wrote down code to... read from file...
    It will work nicely with shree solution.

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    May 2001
    Posts
    4

    Re: Ahhh... creating a text file...

    ok, that kinda works but what i need is a seperate text file for each student... and it creates an "MathsTutitionbob" (when Username = bob)

    plus, it stores it in a directory below the one i want... grrr...

    But thanks alot!

    I wanted to be a procrastinator, but never got round to it.

    <i> Remove NOSPAM_ to email me </i>

  8. #8
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Ahhh... creating a text file...


    dim sFileName as string
    dim sFilePath as string
    sFileName = "MathsTuition" & txtUser.Text
    sFilePath = App.Path & "\yourDir\"
    Open sFilePath & sFileName for Output as #1
    print #1, txtUser.Text
    print #1, txtPass.text
    Close #1








  9. #9
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Ahhh... creating a text file...

    I think this is closer to what you wanted. Missed the \ earlier.

    Open App.Path & "\" & txtUser.Text & ".txt" for Output as #1
    print #1, txtUser.Text
    print #1, txtPass.text
    Close #1





  10. #10
    Join Date
    May 2001
    Posts
    4

    Bingo!

    Thanks Shree, Thanks a lot, it works!!!

    I wanted to be a procrastinator, but never got round to it.

    <i> Remove NOSPAM_ to email me </i>

  11. #11
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Ahhh... creating a text file...

    'you can hard code the path
    dim yourPath as string
    yourpath = "c:\yourdir\yoursubdir\"
    'you should chek for its existance:
    if dir(yourpath,vbdirectory) <> "" then
    Open yourpath.Path & "pass.txt" for append as #1
    print #1, txtUser.Text
    print #1, txtPass.text
    Close #1
    end if
    'and if you want a different txt file for each student
    '(you sure you want?)
    you can do as follow:
    Option Explicit
    Dim yourpath As String

    Private Sub Command1_Click()
    'use username to write a file
    yourpath = "c:"
    Open yourpath.Path & "\" & Text1.Text For Output As #1
    'if it does not exist= created.
    'if it exists= replaced
    'store only password
    Print #1, txtPass.Text
    Close #1

    End Sub

    Private Sub Command2_Click()
    Dim vntInput As String
    'to retrieve:
    yourpath = "c:"

    If Dir(yourpath & "\" & Text1.Text) <> "" Then
    Open yourpath.Path & "pass.txt" For Input As #1
    Line Input #1, vntInput
    Close #1
    'in vbinput you have passowrd
    If Text2.Text <> vbinput Then
    MsgBox "Wrong Password"
    End If
    Else
    MsgBox "user not registered. Create the file, before."
    End If

    End Sub





    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  12. #12
    Join Date
    Apr 2000
    Posts
    737

    Re: Ok, Nearly there....

    download the source code and unzip to anyway. look at the function that you are interested in.


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

Featured