![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Use both read and write operation to the same file I have a file to which I'll need to at first read until it locates the last line of string. According to content of the last line of string, I'll need to write something to this file right after the last line of string. This requires that I'll need a StreamReader at first (to read one line at a time), until the last line is read. Then I'll need a StreamWriter to point to the end of the file, and write to it. Questions: 1) Is there a single Stream type of object that I can use for both reading and writing, because I'll need to write at the point where read operation is done? 2) Is there a method in the Stream object that reads only the last line from a file (instead of reading all of the lines of strings)? |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Use both read and write operation to the same file Hi, There is no StreamDoSomething http://msdn.microsoft.com/en-us/library/system.io.aspx A stream is in fact a stream of bytes. By the textstreams this can be delimited by the CR and LF to break in so called lines of strings. Thereore you can not get the last bytes of a string that starts with a CR and LF, just because you don't know what is the last. Cor http://msdn.microsoft.com/en-us/library/system.io.aspx "Curious" <fir5tsight@xxxxxx> schreef in bericht news:2aeb3d11-756c-4f57-97b5-6d0817203c0f@xxxxxx Quote: >I have a file to which I'll need to at first read until it locates the > last line of string. > > According to content of the last line of string, I'll need to write > something to this file right after the last line of string. > > This requires that I'll need a StreamReader at first (to read one line > at a time), until the last line is read. > > Then I'll need a StreamWriter to point to the end of the file, and > write to it. > > Questions: > > 1) Is there a single Stream type of object that I can use for both > reading and writing, because I'll need to write at the point where > read operation is done? > > 2) Is there a method in the Stream object that reads only the last > line from a file (instead of reading all of the lines of strings)? |
My System Specs![]() |
| | #3 (permalink) |
| | RE: Use both read and write operation to the same file Unless the file size is large, I think the easiest way would be the following: string [] lines = System.IO.File.ReadAllLines("somefile.txt"); // do some manipulations with lines[lines.Count - 1]; System.IO.File.WriteAllLines("somefile.txt", lines); "Curious" wrote: Quote: > I have a file to which I'll need to at first read until it locates the > last line of string. > > According to content of the last line of string, I'll need to write > something to this file right after the last line of string. > > This requires that I'll need a StreamReader at first (to read one line > at a time), until the last line is read. > > Then I'll need a StreamWriter to point to the end of the file, and > write to it. > > Questions: > > 1) Is there a single Stream type of object that I can use for both > reading and writing, because I'll need to write at the point where > read operation is done? > > 2) Is there a method in the Stream object that reads only the last > line from a file (instead of reading all of the lines of strings)? > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Use both read and write operation to the same file Thanks for sharing! |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Use both read and write operation to the same file Good to know that there is a way to get all of the lines in a single operation, and get the last line! |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Read from a txt, query AD, write to an xls file? | VB Script | |||
| read/write/create 'dbf' file | .NET General | |||
| How to: Dual use USB drive ---> Read/Write from Computer and Read from "Stand Alone DVD Player" | Vista General | |||
| How to read and write a text file? | VB Script | |||
| How to open a file for read/write access in Program Files directory | Vista security | |||