|
-
Feb 16th, 2006, 12:20 PM
#1
Thread Starter
New Member
Modifying individual lines in a text file
Modifying individual lines in a text file
Hello,
This question has to do with modifying a .txt file.
I would like to choose a specific line in the text file and replace it with new text without modifying the layout of the rest of the file. In the file, there is no consistency in the number of characters per line.
VB6 code might look something like this for a file 11 lines in length where only the 10th line is changed:
VB Code:
Dim WriteFile1 As Integer
Dim i As Integer
WriteFile1 = 10
Open “Data.txt” For Output As WriteFile1
For i = 0 To 9
Print #WriteFile1, [url]www.InputS1(i[/url])
Next
Print #WriteFile1, “Hello World 1 4 3 5 3 5”
Print #WriteFile1, [url]www.InputS1(11[/url])
Close WriteFile1
How might this be accomplished in .NET? Thanks in advance for any help.
-
Feb 16th, 2006, 12:39 PM
#2
Re: Modifying individual lines in a text file
You can't just pick a line in a text file and modify it in .NET. You would need to read the entire file in with a StreamReader and then write out again with a StreamWriter. You can make changes as you read it in, while it's in memory or as you write it out. There are a number of variations on a theme but they all do basically the same thing.
-
Feb 16th, 2006, 01:27 PM
#3
Thread Starter
New Member
Re: Modifying individual lines in a text file
Thanks for the quick reply. This is what I came up with. I haven’t had time to test it out but the syntax seems somewhat clumsy. Any suggestions?
VB Code:
Dim fileName As String
Dim strLine As String
Dim i As Integer
fileName = Data.txt
fileName2 = Temp.txt
Dim stream As StreamReader
stream = New StreamReader(fileName)
Dim outStream As StreamWriter
outStream = New StreamWriter(fileName2)
For i = 1 To 9
strLine = stream.ReadLine
outStream.WriteLine(strLine)
Next
outStream.WriteLine("Hello World 1 4 3 5 3 5")
strLine = stream.ReadLine
strLine = stream.ReadLine
outStream.WriteLine(strLine)
System.IO.File.Copy(Temp.txt, Data.txt)
Thanks again for your help.
-
Feb 16th, 2006, 03:54 PM
#4
Re: Modifying individual lines in a text file
wasn't sure what all you were trying to accomplish, but here is a little cleanup. There is an option to compare the string when reading it in, and if it equals the text you want, it writes another line to the file, like an insert.
VB Code:
'this reads and writes the files from the application directory
Dim stream As New System.IO.StreamReader(Application.StartupPath & "\data.txt")
Dim outStream As New System.IO.StreamWriter(Application.StartupPath & "\temp.txt")
'reads line by line to end of file
While stream.Peek <> -1
Dim mystring As String = stream.ReadLine
MessageBox.Show(mystring) 'just used to show you it loops line by line
outStream.WriteLine(mystring)
'this inserts a string once it hits the text you want
If mystring = "thetextIwant" Then
outStream.WriteLine("Hello World 1 4 3 5 3 5")
End If
End While
'closes readers and writers
stream.Close()
outStream.Close()
'not sure why you were copying the file at the end, took that out of this example
Of course, you would essentially have to type out the entire string in full if you want it to match inside the loop, just used it as a quick example. There are other ways, like finding if a string contains certain text that you want....
-
Feb 16th, 2006, 04:15 PM
#5
Thread Starter
New Member
Re: Modifying individual lines in a text file
I’m trying to find a nice way to modify selected lines in a text file without causing the rest of the file to get all out of whack. So for example I had a text file (data.txt) like this:
I
Would
Really
Like
For
This
Program
To
Work
And in line number 2 I want to change ‘Would’ to ‘Do’ and in line 4 I want to change ‘Like’ to ‘Need’ It would re-create data.txt to look like this:
I
Do
Really
Need
For
This
Program
To
Work
I’ve found good information on appending data to the end of a file, as well as re-writing the file line by line starting at the top, but for some reason I can’t figure out the best way to count down to the correct line, replace the text and leave the rest of the data alone. It has got to be straightforward as I would guess that this is a common task – but so far no luck.
I really appreciate everyone’s help.
(EDIT) One of the problems is that each line can be changed in a non-predictable manner, so at any given time the program may not know what text is in a line, only the line’s position in the file.
Last edited by CRHY; Feb 16th, 2006 at 04:23 PM.
-
Feb 16th, 2006, 04:30 PM
#6
Re: Modifying individual lines in a text file
Then its just a matter of setting a counter in the loop, once the counter is at the variable you want, write the line you want. Below has an insertposition and an insertstring to test, should insert it at the line you want.
VB Code:
'keep code above, replace While loop with below, along with added test variables
Dim Counter as Integer = 0
Dim InsertPosition as integer = 2 'line number
Dim InsertString as string = "Test"
While stream.Peek <> -1
Dim mystring As String = stream.ReadLine
If Counter = InsertPosition -1 then
outStream.WriteLine(InsertString)
End If
outStream.WriteLine(mystring)
Counter = Counter + 1
End While
***Note - This was hand coded and not tested...
-
Feb 16th, 2006, 04:50 PM
#7
Thread Starter
New Member
Re: Modifying individual lines in a text file
Yes! Thats what I'm thinking about.
Maybe this:
VB Code:
Dim stream As New System.IO.StreamReader(Application.StartupPath & "\data.txt")
Dim outStream As New System.IO.StreamWriter(Application.StartupPath & "\temp.txt")
Dim Counter As Integer = 0
Dim InsertPosition As Integer = 2 'line number
Dim InsertString As String = "Test"
While stream.Peek <> -1
Dim mystring As String = stream.ReadLine
If Counter = InsertPosition Then
outStream.WriteLine(InsertString)
Else
outStream.WriteLine(mystring)
End If
Counter = Counter + 1
End While
stream.Close()
outStream.Close()
System.IO.File.Copy(Temp.txt, Data.txt)
(EDIT) Is there any way to do this in memory without creating a separate temp file?
Last edited by CRHY; Feb 16th, 2006 at 04:56 PM.
-
Feb 16th, 2006, 05:11 PM
#8
Re: Modifying individual lines in a text file
Yes, if you read it all into an array of strings, you can then modify any element of the array, and write it all directly back to the same file. No need for a temp.
However, it seems like you shouldn't need to do even that. I don't have a working system handy, or I would check it out, but in general, read to the line you want, then write the changed line. Can this be done with one file? It seems that it should, but I would want to test it, and I can't.
My usual boring signature: Nothing
 
-
Feb 16th, 2006, 05:27 PM
#9
Re: Modifying individual lines in a text file
I don't think so, shaggy. Think you would just have to read them all in, then modify, then write them all back to the same file (like you said at first). If anyone finds a way to not have to do that, I would give them props because I am not aware of any method.
-
Feb 16th, 2006, 05:40 PM
#10
Re: Modifying individual lines in a text file
You certainly can't open a StreamWriter on a file that already has a StreamReader open on it. A good option to do it without a temp file might be like this:
VB Code:
Dim filePath As String 'Store fully qualified file path here.
Dim fileReader As New IO.StreamReader(filePath)
Dim textWriter As New IO.StringWriter
Do Until fileReader.Peek() = -1
textWriter.WriteLine(fileReader.ReadLine())
Loop
fileReader.Close()
Dim fileWriter As New IO.StreamWriter(filePath)
fileWriter.Write(textWriter.ToString())
textWriter.Close()
fileWriter.Close()
Obviously you'd need to change the contents of the loop to do what you want but you get the idea. I'm not sure whether you can call ToString on a StringWriter after calling Close or not, but I'll leave that up to others to test if they want to.
This isn't much different to reading the whole file with ReadToEnd and then writing out line by line but this way you don't have to worry about breaking up the lines after reading them as a block.
-
Feb 16th, 2006, 05:44 PM
#11
Re: Modifying individual lines in a text file
I am in no position to check this, but on further thought, I can't think of a good way to do what I was thinking of.
By the way, it may seem computationally costly to read in a bunch of lines, then write them back out. As long as the file size is not particularly large (not more than a few hundred lines max), you won't notice the time taken to do this, so it isn't really an issue of concern.
One alternative, if you expect to do this repeatedly, is to read the file in early on, make many changes, then write the file out once all changes have been made. Makes it appear even faster, since the read and write are separated in time.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|