vbMarkO Posted September 15, 2005 Posted September 15, 2005 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 Quote Visual Basic 2008 Express Edition!
Machaira Posted September 15, 2005 Posted September 15, 2005 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. Quote Here's what I'm up to.
vbMarkO Posted September 15, 2005 Author Posted September 15, 2005 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 Quote Visual Basic 2008 Express Edition!
JTDPublix Posted September 15, 2005 Posted September 15, 2005 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 :) Quote
vbMarkO Posted September 17, 2005 Author Posted September 17, 2005 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 Quote Visual Basic 2008 Express Edition!
vbMarkO Posted September 17, 2005 Author Posted September 17, 2005 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 Quote Visual Basic 2008 Express Edition!
Diesel Posted September 18, 2005 Posted September 18, 2005 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.