Results 1 to 11 of 11

Thread: *resolved, thanks* How can I move to the first line in the file

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    *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!!

  2. #2
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245
    Use File.OpenText and the WriteLine method of the StreamWriter class.
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  4. #4

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  5. #5
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245
    Whoops!

    Try this:

    VB Code:
    1. ' Create an instance of StreamWriter to write text to a file.
    2. Dim sw As StreamWriter = New StreamWriter("TestFile.txt")
    3. ' Add some text to the file.
    4. sw.Write("This is the ")
    5. sw.WriteLine("header for the file.")
    6. sw.WriteLine("-------------------")
    7. ' Arbitrary objects can also be written to the file.
    8. sw.Write("The date is: ")
    9. sw.WriteLine(DateTime.Now)
    10. sw.Close()
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

  6. #6

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    hihi, but that doesnt APPEND to the file that clears eveyrthing in the file and writes the new content. Now I could read the whole file before I write any new data to it, but that kinda sounds stupid....
    there is a SEEK() function but it only works with the old form vb functions (open/close) and I dont really wanna use them.... any way to append to the begining of the file? hmm I thought it's really simple

    thanks for the replies btw
    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!!

  7. #7
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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

  8. #8

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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:
    1. Dim sw As StreamWriter
    2.  
    3.         If File.Exists(journalPath) Then
    4.             sw = File.AppendText(journalPath)
    5.         Else
    6.             sw = File.CreateText(journalPath)
    7.         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!!

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Public Sub AppendToFront(ByVal filename As String, ByVal text As String)
    2.         'open the fiel for read.write
    3.         Dim fs As New IO.FileStream(filename, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
    4.         Dim sr As New IO.StreamReader(fs)
    5.         'read old data
    6.         Dim oldText As String = sr.ReadToEnd
    7.         Dim sw As New IO.StreamWriter(fs)
    8.         'move to start of file
    9.         fs.Position = 0
    10.         'write new text in front
    11.         sw.WriteLine(text)
    12.         'write old text after
    13.         sw.WriteLine(oldText)
    14.         'flushes writer to file
    15.         sw.Flush()
    16.         'clean up
    17.         fs.Close()
    18.     End Sub
    19.  
    20. 'syntax
    21.         AppendToFront("..\sample.txt", TextBox2.Text)
    Last edited by Edneeis; Jul 11th, 2003 at 12:46 AM.

  10. #10

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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
  •  



Click Here to Expand Forum to Full Width