stream & streamreader

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andrewcw

    stream & streamreader

    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(fiPat h, FileMode.Open,
    FileAccess.Read Write,FileShare .None);
    System.IO.Strea mReader Tin = new System.IO.Strea mReader
    (stream);
    string existText = Tin.ReadToEnd() ;
    fileContent=Rep lacementString
    (oldStr,newStr, "","",existText );
    // OK I HAVE MY NEW STRING...
    System.IO.Strea mWriter Tout = new System.IO.Strea mWriter
    (stream);
    Tout.Write(file Content); // 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();

    [color=blue]
    >-----Original Message-----
    >Currently I received a great solution to unique file
    >access like this:
    >FileStream stream = null;
    >stream = File.Open(fiPat h, FileMode.Open,
    >FileAccess.Rea dWrite,FileShar e.None);
    >
    >But to read the stream- it seems I have to do this:
    >byte[] b = new byte[1024];
    >UTF8Encoding temp = new UTF8Encoding(tr ue);
    >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
    ><<StreamRead er sr2 = File.OpenText(f iPath);
    >string foo2= sr2.ReadToEnd() ;[color=green][color=darkred]
    >>>[/color][/color]
    >BUT I cant seem to cast stream into streamReader
    >( StreamReader sr2 =( StreamReader)st ream; // wont work,
    >cannot convert ....
    >
    >I have the same issue when it comes to writing the[/color]
    changes[color=blue]
    >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 !!!!
    >
    >
    >
    >
    >.
    >[/color]
  • andrewcw

    #2
    stream &amp; streamreader

    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

    [color=blue]
    >-----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(fiPat h, FileMode.Open,
    >FileAccess.Rea dWrite,FileShar e.None);
    >System.IO.Stre amReader Tin = new System.IO.Strea mReader
    >(stream);
    >string existText = Tin.ReadToEnd() ;
    >fileContent=Re placementString
    >(oldStr,newStr ,"","",existTex t);
    >// OK I HAVE MY NEW STRING...
    >System.IO.Stre amWriter Tout = new System.IO.Strea mWriter
    >(stream);
    >Tout.Write(fil eContent); // just sticks back in the stream
    >Tout.Close() ; // IS THIS ACTUALLY UPDATING THE[/color]
    STREAM ??[color=blue]
    >Tin.Close();
    >// how do I persist the revised stream to file ??
    >stream.Close() ;
    >
    >[color=green]
    >>-----Original Message-----
    >>Currently I received a great solution to unique file
    >>access like this:
    >>FileStream stream = null;
    >>stream = File.Open(fiPat h, FileMode.Open,
    >>FileAccess.Re adWrite,FileSha re.None);
    >>
    >>But to read the stream- it seems I have to do this:
    >>byte[] b = new byte[1024];
    >>UTF8Encodin g temp = new UTF8Encoding(tr ue);
    >>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[/color][/color]
    the[color=blue][color=green]
    >>string by doing something like
    >><<StreamReade r sr2 = File.OpenText(f iPath);
    >>string foo2= sr2.ReadToEnd() ;[color=darkred]
    >>>>[/color]
    >>BUT I cant seem to cast stream into streamReader
    >>( StreamReader sr2 =( StreamReader)st ream; // wont work,
    >>cannot convert ....
    >>
    >>I have the same issue when it comes to writing the[/color]
    >changes[color=green]
    >>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 !!!!
    >>
    >>
    >>
    >>
    >>.
    >>[/color]
    >.
    >[/color]

    Comment

    Working...