Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add lines to an exsisting textfile 3

Status
Not open for further replies.

uprocker2

Programmer
Dec 26, 2004
34
BE
Hi
I am making a user login / create new user program that uses winsock whit a client/server program when the user enters a new password into the clienprogram the data is transferd to the server.<-- this I can do
but...
when the "new data" arives i want the server to save it in a textfile that can be read later. the second time I load the server I want the server to exept the new user and be able to add new accounts.

***BJAY***
 
uprocker2,

Add Microsoft Scripting Runtime reference to your project and call the funcion below, for example:

AddLineToTextFile "C:\MyLogFile.txt","My New Line."

Private Sub AddLineToTextFile(ByVal pstrFullFileName As String, ByVal pstrNewLine As String)
Dim objFSO As New FileSystemObject
Dim objFile As File
Dim objTextStream As TextStream

Set objTextStream = objFSO.OpenTextFile(FileName:=pstrFullFileName, _
IOMode:=ForAppending, _
Format:=TristateFalse)


objTextStream.WriteLine pstrNewLine
objTextStream.Close

Set objFSO = Nothing
Set objFile = Nothing
Set objTextStream = Nothing
End Sub
 
ow thanx for ure greate help apresiate it 1 more thing dow
how do i set the screenresualtation of a form to fullscreen?
 
uprocker2,

1.In my previous example you may remove objFile from the procedure.

2.To set Microsoft Scripting Runtime reference to your VB project you need:

- Click Project/References
- Locate and check Microsoft Scripting Runtime in the list of the available references. It should be scrrun.dll
- Click OK button

If you don't have this dll you can probably download it from here:

3. I don't know what "screenresualtation" means.

vladk
 
>screenresualtation

Have a look at the form's WindowState property
 
This is how you can simply write to a file.

set userscreen$ to the user name to save
set userscreen$ to the user pass to save
This will simply add the new user and pass to the file:
Code:
Open "password.dat" For Output As #1
Print #1, userscreen$
Print #1, userpass$
Close #1



To load previously saved user:
Code:
Open "password.dat" For input As #1
Input #1, userscreen$
Input #1, userpass$
Close #1

SkyFighter
skyfighta@hotmail.com
Get the fun out of life!

 
thx for all the help u guys
this site rocks
btw i am getting help from lived now .. thx again

 
1 thing....

If you want to add to the end of a file... you need to use Append in place of Output, Output will overwite the file if it already exists... (no questions asked ;-))

Code:
Open "password.dat" For [b]Append[/b] As #1
Print #1, UserScreen
Print #1, UserPass
Close #1


Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Hi again
thx for all the help untill now...
antoher thing...
if i save it whit append like db101 sead
how do i then do:
when there is a user that logs in whit the username and password that already exist how do i load THAT line of
userscreen and userpass that matches the user that's trying to log on the program?
and how do i save date assosiated whit the userscreen and userpass.
(exemple:when user xxx logs in ,he walks around in a game and logs out the current position of the player is saved in the data file whit assosiation of the user xxx.so the next time the user logs in again he will find hisself on the place he logged out,I also need this for the items he is holding in his inverntory)



_-_Uprocker2_-_
MSN = Uprocker2@hotmail.com
 
I think I might have answered this for you already in thread222-973974

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
ow right one more question do,
its a very low sucruity app for adims
but is it also possible to create a .dat of .txt(i preffeare .dat)that is read onli and is accesible by the app so the app can ad users and passes but it can't be donne by doing it manualy from the harddisk.is it also pissible to make the "new user program" like in Thread222-973974 so the other users can't just change passwords.
I want to user vb code for this couse I am not willing to go and learn xml.srry about that :S
thx for fast reply
 
I basically gave you all you needed as far as learning xml goes...

I think the thing that scares most people away is not being able to believe how simple xml really is...

it is just a structured text file... (nothing more) it, itself, does nothing. it just provides a standard structure to write the data in, and tools to parse it later on to retrieve the stored data...

Here is an example...
Code:
<start>
  <somenode>Text Goes Here</somenode>
</start>

And another example...
Code:
<start>
  <somenode>
    <somenode>
      <somenode>Text Goes Here</somenode>
    </somenode>
  </somenode>
  <somenode>
    <SubNode>Text Goes Here</SubNode>
    <SubNode>Text Goes Here</SubNode>
    <SubNode>Text Goes Here</SubNode>
    <SubNode>Text Goes Here</SubNode>
  </somenode>
  <somenode>Text Goes Here</somenode>
</start>

You create your own tags, your own structure, ect...
It's gotta be the EASIEST 'language' to learn...

Look at the examples I gave you on the mentioned thread and give it a try... You will most definitely like it ;-)

your file can be as simple as this:
Code:
<users>
  <user>
    <name>Bob</name>
    <pass>nahNAHnah</pass>
  </user>
  <user>
    <name>Tom</name>
    <pass>HeHeHe</pass>
  </user>
  <user>
    <name>Joe</name>
    <pass>NoTtElLiNg</pass>
  </user>
</users>


As far as the users changing their pass, it would be the same code, just require them to be logged it to change it... (and their pass only, of course)

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
great so where do i put these xml codes whitch software????
 
Ummm....

These sets of code go in VB...

check into XML at
or... the XML forum on this site... Forum426

The DOM (Document Object Model) is very easy to setup and manipulate in VB...

The Reference For It is:
Microsoft XML, version 2.0
*version may vary

Create New Dom...
Code:
Dim MyDom As DOMDocument
Set MyDom = New DOMDocument
MyDom.appendChild MyDom.createElement("Users")

create an element...
Code:
Dim Elem As IXMLDOMElement
Set Elem = MyDom.createElement("User")
MyDom.documentElement.appendChild(Elem)
or like this ...
Code:
Dim Elem As IXMLDOMElement
Set Elem = MyDom.documentElement.appendChild(MyDom.createElement("User"))

add sub element...
Code:
Dim SubElem As IXMLDOMElement
Set SubElem = MyDom.createElement("Name")
Elem.appendChild(SubElem)
SubElem.Text = "UserName"
or like this ...
Code:
Elem.appendChild(MyDom.createElement("Name")).Text = "UserName"

To save a file...
Code:
MyDom.Save "C:\Path\file.xml"

Then to load a file...
Code:
Dim MyDom As DOMDocument
Set MyDom = New DOMDocument
With MyDom
  .Async = False
  .Load "C:\Path\file.xml"
End With

To get all of the user nodes...
Code:
Dim X As IXMLDOMElement
For Each X In MyDom..documentElement.getElementsByTagName("User")
  ...
Next

To Get Children of an Element...
Code:
Dim Y As IXMLDOMElement
For Each Y In X.childNodes
  ...
Next

The XML "code" (if you want to call it that) such as:
Code:
<users>
  <user>
    <name>Bob</name>
    <pass>nahNAHnah</pass>
  </user>
  <user>
    <name>Tom</name>
    <pass>HeHeHe</pass>
  </user>
  <user>
    <name>Joe</name>
    <pass>NoTtElLiNg</pass>
  </user>
</users>
goes in a file with an extention of your choice... There is code listed above to create, Save, And Load these files...



Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
And if you want an example... (Merry Christmas ;-))

*Requires Reference...
Microsoft XML, version 2.0
*version may vary

Insert this into a Module...
Code:
Private myDom As DOMDocument

'Return True If Successful
Function LoadUsers(File As String) As Boolean
  Set myDom = New DOMDocument
  'If File Exists...
  If Len(Dir(File)) Then
    'Load File
    myDom.Async = False
    myDom.Load File
    LoadUsers = True
  Else
    'Else create New
    myDom.appendChild myDom.createElement("Users")
    LoadUsers = True
  End If
End Function

'Return True If Successful
Function SaveUsers(File As String) As Boolean
  If myDom Is Nothing Then Exit Function
  If File <> "" Then
    myDom.Save File
    SaveUsers = True
  End If
End Function

'Return True If Successful
Function AddUser(User As String, Pass As String) As Boolean
  If User = "" Or Pass = "" Then Exit Function
  If myDom Is Nothing Then Exit Function
  Dim X As IXMLDOMElement
  Dim Y As IXMLDOMElement
  For Each X In myDom.documentElement.getElementsByTagName("User")
    For Each Y In X.childNodes
      If Y.tagName = "Name" Then
        'user exists, exit sub... return false (default)
        If Y.Text = User Then Exit Function
      End If
    Next
  Next
  'User did not exist, create and return true...
  Set X = myDom.documentElement.appendChild(myDom.createElement("User"))
  X.appendChild(myDom.createElement("Name")).Text = User
  X.appendChild(myDom.createElement("Pass")).Text = Pass
  AddUser = True
End Function

'Return True If Successful
Function CheckUser(User As String, Pass As String) As Boolean
  If User = "" Or Pass = "" Then Exit Function
  If myDom Is Nothing Then Exit Function
  Dim X As IXMLDOMElement
  Dim Y As IXMLDOMElement
  Dim UOk As Boolean, POk As Boolean
  For Each X In myDom.documentElement.getElementsByTagName("User")
    For Each Y In X.childNodes
      If Y.tagName = "Name" Then UOk = (Y.Text = User)
      If Y.tagName = "Pass" Then POk = (Y.Text = Pass)
      'If User and Pass both match, Return True and Exit
      If UOk And POk Then
        CheckUser = True
        Exit Function
      End If
    Next
    'Reset, Just in case...
    POk = False
    UOk = False
  Next
End Function

'Return True If Successful
Function ChangePass(User As String, Pass As String) As Boolean
  If User = "" Or Pass = "" Then Exit Function
  If myDom Is Nothing Then Exit Function
  Dim X As IXMLDOMElement
  Dim Y As IXMLDOMElement
  Dim UOk As Boolean
  For Each X In myDom.documentElement.getElementsByTagName("User")
    For Each Y In X.childNodes
      If Y.tagName = "Name" Then UOk = (Y.Text = User)
      If UOk Then
        If Y.tagName = "Pass" Then
          Y.Text = Pass
          ChangePass = True
          Exit Function
        End If
      End If
    Next
    'Reset, Just in case...
    UOk = False
  Next
End Function

Then create a Form with the following controls:
TextBox :: Text1
TextBox :: Text2
CommandButton :: Command1
CommandButton :: Command2
CommandButton :: Command3


Then Add this to the code area...
Code:
'Change Path, Name, and Extention to suit your needs...
Const UserFilePath = "C:\UserData.Dat"

Private Sub Form_Load()
  Text1.Text = "UserID"
  Text2.Text = "Password"
  Command1.Caption = "New User"
  Command2.Caption = "Check User"
  Command3.Caption = "Change Pass"
  Me.Caption = "User Add/Check/Mod Test :: Data File " & IIf(LoadUsers(UserFilePath), "Loaded", "Created")
End Sub

Private Sub Form_Unload(Cancel As Integer)
  If Not SaveUsers(UserFilePath) Then
    MsgBox "Warning, Data file not saved."
  End If
End Sub

Private Sub Command1_Click()
  MsgBox "Add User " & IIf(AddUser(Text1.Text, Text2.Text), "Successful", "Failed")
End Sub

Private Sub Command2_Click()
  MsgBox "Check User " & IIf(CheckUser(Text1.Text, Text2.Text), "Successful", "Failed")
End Sub

Private Sub Command3_Click()
  MsgBox "Change Pass " & IIf(ChangePass(Text1.Text, Text2.Text), "Successful", "Failed")
End Sub

You can change the path in UserFilePath if you want...

Test it out with all of the buttons, then close it, and open the generated file in a text editor... (Notepad)

run the program again and check a few of the created IDs...

Hope This Helps ;-)

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top