stream & streamreader

A

andrewcw

OK I am half way there - I can manipulate the stream
without the byte issue like this - but is this the way to
push the new values back into the stream & write out the
stream without resorting to byte conversion ??

FileStream stream = null;
stream = File.Open(fiPath, FileMode.Open,
FileAccess.ReadWrite,FileShare.None);
System.IO.StreamReader Tin = new System.IO.StreamReader
(stream);
string existText = Tin.ReadToEnd();
fileContent=ReplacementString
(oldStr,newStr,"","",existText);
// OK I HAVE MY NEW STRING...
System.IO.StreamWriter Tout = new System.IO.StreamWriter
(stream);
Tout.Write(fileContent); // just sticks back in the stream
Tout.Close(); // IS THIS ACTUALLY UPDATING THE STREAM ??
Tin.Close();
// how do I persist the revised stream to file ??
stream.Close();

-----Original Message-----
Currently I received a great solution to unique file
access like this:
FileStream stream = null;
stream = File.Open(fiPath, FileMode.Open,
FileAccess.ReadWrite,FileShare.None);

But to read the stream- it seems I have to do this:
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
string footest;
while (stream.Read(b,0,b.Length) > 0)
footest= temp.GetString(b);
footest = footest.TrimEnd('\0');
int footestlen = footest.Length;
}

[ I'd prefer to not worry about the size, or trimming the
string by doing something like
<<StreamReader sr2 = File.OpenText(fiPath);
string foo2= sr2.ReadToEnd();BUT I cant seem to cast stream into streamReader
( StreamReader sr2 =( StreamReader)stream; // wont work,
cannot convert ....

I have the same issue when it comes to writing the changes
back out. Is it really that messy, or have I overlooked
teh obvious for simple text IO ( and maintain the unique
access that stream provides ) THANKS !!!!




.
 
A

andrewcw

OK By resetting the position of the stream before the
write and then resetting it again to check the stream
contents I find my updates in the stream, but how to get
them out simply to be written to the file ??

?? maybe I should post as new thread .... and reformulate
the question

-----Original Message-----
OK I am half way there - I can manipulate the stream
without the byte issue like this - but is this the way to
push the new values back into the stream & write out the
stream without resorting to byte conversion ??

FileStream stream = null;
stream = File.Open(fiPath, FileMode.Open,
FileAccess.ReadWrite,FileShare.None);
System.IO.StreamReader Tin = new System.IO.StreamReader
(stream);
string existText = Tin.ReadToEnd();
fileContent=ReplacementString
(oldStr,newStr,"","",existText);
// OK I HAVE MY NEW STRING...
System.IO.StreamWriter Tout = new System.IO.StreamWriter
(stream);
Tout.Write(fileContent); // just sticks back in the stream
Tout.Close(); // IS THIS ACTUALLY UPDATING THE STREAM ??
Tin.Close();
// how do I persist the revised stream to file ??
stream.Close();

-----Original Message-----
Currently I received a great solution to unique file
access like this:
FileStream stream = null;
stream = File.Open(fiPath, FileMode.Open,
FileAccess.ReadWrite,FileShare.None);

But to read the stream- it seems I have to do this:
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
string footest;
while (stream.Read(b,0,b.Length) > 0)
footest= temp.GetString(b);
footest = footest.TrimEnd('\0');
int footestlen = footest.Length;
}

[ I'd prefer to not worry about the size, or trimming the
string by doing something like
<<StreamReader sr2 = File.OpenText(fiPath);
string foo2= sr2.ReadToEnd();BUT I cant seem to cast stream into streamReader
( StreamReader sr2 =( StreamReader)stream; // wont work,
cannot convert ....

I have the same issue when it comes to writing the changes
back out. Is it really that messy, or have I overlooked
teh obvious for simple text IO ( and maintain the unique
access that stream provides ) THANKS !!!!




.
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top