Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

WHat I am needing is this;

 

I have a text file of names..... I need to search through the file find a specific name such as below then add a employee number at the end of that string as seen also below

 

Doe, John <----- Name searched for

 

Doe, John 12523 <------- now the text file has been changed only on this specifc line of which the user called for.

 

NOTE: I have written the code to search and find the name in the textfile

just need to know how to replace it with the new string that will contain the name and the added Employee number.

 

vbMarKO

Visual Basic 2008 Express Edition!
Posted
You need to write out the whole file. What I would probably do is read the file into an array (each line is an element), change the line then write the array back out to the file.
Here's what I'm up to.
Posted

That makes sense....

 

I didnt think of that, that would simplify it. Ill give that a go!

 

Ofcourse I am open to anyother sugestions from anyone that might feel they have a better way or just another way of doing it.

 

vbMarkO

Visual Basic 2008 Express Edition!
Posted

Machaira's method would work just fine.

 

Here is another option for you:

 

1.) Read entire file in.

2.) Do a replace on the name like this:

 

searchName = "John Doe"

empNum = "12345"

Replace(fileContents, searchName, searchName & " " & empNum)

 

It's up to you at this point :)

Posted
Machaira's method would work just fine.

 

Here is another option for you:

 

1.) Read entire file in.

2.) Do a replace on the name like this:

 

searchName = "John Doe"

empNum = "12345"

Replace(fileContents, searchName, searchName & " " & empNum)

 

It's up to you at this point :)

 

 

Yes I am using His and its working however I would like to try this as well its always good to try other things as you never know when you might need it.

 

So lets see if I understand this.

 

IN the above example

filecontents is the string that contains the contents of the entire file

searchname contains "John Doe" which is the name you are looking for

the final searchname & " " & empnum is the name looked for and employee number concantinated.

 

Ok I think I got it I will give this a go too

 

Thanx

vbMarkO

Visual Basic 2008 Express Edition!
Posted

ok I gave it a try but I must be missing something or its not working for one reason or another.

 

Tell me if I have it right!!

 

Dim strName As String
       strName = TextBox2.Text
       Dim strText As String
       For i As Integer = 0 To list1.Items.Count - 1
           strText = strText & list1.Items.Item(i) & vbCrLf
       Next
       Replace(strText, strName, strName & " " & TextBox1.Text)
       Debug.Write(strText)

Shouldnt this work?

What am I missing?

Oh btw I simply use a listbox with item names in it... this may be the problem but I dont see why.

the list Items as follows

Doe, John

Sue, Sally

Jones, Jim

ect ect ect ect

maybe I didnt add the listbox Items to the string right but when I debug the string wrote out properly

vbMarkO

Visual Basic 2008 Express Edition!
Posted

If you want a more efficient way of doing something like this, you can write to the file in binary mode, sending it structures instead of just string data. At that point you can read the records into structures, change the structure you want and just write that structure to the file. This file access is known as random access. You'll only be modifying one record everytime.

 

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemIOFileStreamClassTopic.asp

 

http://www.codeproject.com/csharp/objserial.asp

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...