Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Use both read and write operation to the same file

Reply
 
Old 05-18-2008   #1 (permalink)
Curious


 
 

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 SpecsSystem Spec
Old 05-19-2008   #2 (permalink)
Cor Ligthert[MVP]


 
 

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 SpecsSystem Spec
Old 05-19-2008   #3 (permalink)
Family Tree Mike


 
 

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 SpecsSystem Spec
Old 05-19-2008   #4 (permalink)
Curious


 
 

Re: Use both read and write operation to the same file

Thanks for sharing!
My System SpecsSystem Spec
Old 05-19-2008   #5 (permalink)
Curious


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46