By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,616 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,616 IT Pros & Developers. It's quick & easy.

stream & streamreader

P: n/a
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

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


.

Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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 thestring 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 !!!!


.

.

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.