|
-
Jul 9th, 2003, 01:07 PM
#1
*resolved, thanks* How can I move to the first line in the file
that is, using the streamWriter class
I'm opening a textfile file File.AppenText("blah") function. But the append mode adds everything to the end of the file. I want to move to the begining of the file. Is there a seek function?
Last edited by MrPolite; Jul 11th, 2003 at 03:10 AM.
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jul 9th, 2003, 03:16 PM
#2
Addicted Member
Use File.OpenText and the WriteLine method of the StreamWriter class.
-
Jul 9th, 2003, 03:22 PM
#3
Originally posted by Sheppe
Use File.OpenText and the WriteLine method of the StreamWriter class.
interesting interesting thanks
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jul 9th, 2003, 11:55 PM
#4
umm ahem but OpenText returns a streamReader object, and OpenWrite returns fileStream
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jul 10th, 2003, 09:36 AM
#5
Addicted Member
Whoops!
Try this:
VB Code:
' Create an instance of StreamWriter to write text to a file.
Dim sw As StreamWriter = New StreamWriter("TestFile.txt")
' Add some text to the file.
sw.Write("This is the ")
sw.WriteLine("header for the file.")
sw.WriteLine("-------------------")
' Arbitrary objects can also be written to the file.
sw.Write("The date is: ")
sw.WriteLine(DateTime.Now)
sw.Close()
-
Jul 10th, 2003, 01:38 PM
#6
-
Jul 10th, 2003, 01:45 PM
#7
Frenzied Member
Isn't there a way to merge two text files? If there is then you write your new line to a new file then merge it with the other file. Will give it a try and let you know.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Jul 10th, 2003, 11:58 PM
#8
hmm there is streamwriter.BaseStream.Seek() how do I use it
OMG I cant believe I'm turning this dumb... can't figure out the simplest thing in the universe, haha
anyways wasnt there a method that would create a file if it didnt exist, or it would open it if it already exitsted? My way seems kinda akward:
VB Code:
Dim sw As StreamWriter
If File.Exists(journalPath) Then
sw = File.AppendText(journalPath)
Else
sw = File.CreateText(journalPath)
End If
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jul 11th, 2003, 12:42 AM
#9
Really there is no need to find the first line of the file because when you open the file or stream you are at the first line. Although you can change the position through the Position property of the FileStream, for instance returning to the start you'd use fs.Position=0. But there is a problem with that because if you just start writing at that position then you will write over whatever is already there. The only way to append text to the front if a file is read the contents to a variable then move to the front, write the new content line and then rewrite the old content after it. Also you can use the FileMode.OpenOrCreate in making a FileStream to have it automatically open an existing file or create one if there is none yet. So here is a quick function to append text to the front of a file:
VB Code:
Public Sub AppendToFront(ByVal filename As String, ByVal text As String)
'open the fiel for read.write
Dim fs As New IO.FileStream(filename, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
Dim sr As New IO.StreamReader(fs)
'read old data
Dim oldText As String = sr.ReadToEnd
Dim sw As New IO.StreamWriter(fs)
'move to start of file
fs.Position = 0
'write new text in front
sw.WriteLine(text)
'write old text after
sw.WriteLine(oldText)
'flushes writer to file
sw.Flush()
'clean up
fs.Close()
End Sub
'syntax
AppendToFront("..\sample.txt", TextBox2.Text)
Last edited by Edneeis; Jul 11th, 2003 at 12:46 AM.
-
Jul 11th, 2003, 01:10 AM
#10
aah hehe thanks Edneeis. One question though, is it necessary to call the Flush function? Couldnt you just close the stream?
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jul 11th, 2003, 02:55 AM
#11
Probably, try and see. Since I used two Stream Helpers I didn't want one to feel left out if I didn't use it so I closed the filestream instead. I'm pretty sure the StreamWriter/Reader will flush when closed though. Its all basically the samething.
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
|