object initialization ( easy ? ) File Stream

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a function that calls a function

FileStream findata

openCloseTextFileStream(xmldataPath,out findata,true,4,out errorMes); // get a loaded FileStream to use for ReadXM
if (errorMes.Length<1

ds.ReadXml(findata);

findata.Close()

What I do in openCloseTextFileStream is open exclusively the file and and attempt to write the bytes back into the return stream then close the actual fileStream/file - but
I must provide an initial value in the function for my 'out' value, mystream
But if I do this ( within the openCloseTextFileStream function ) : mystream = null; // so I can complil
Then I get Object reference not set to an instance of an object when I attempt to fill it with Data

What is a good way to do this ? Thanks Andrew
 
andrewcw said:
I have a function that calls a function :

FileStream findata;

openCloseTextFileStream(xmldataPath,out findata,true,4,out errorMes);
// get a loaded FileStream to use for ReadXML
if (errorMes.Length<1)
{
ds.ReadXml(findata);
}
findata.Close();

What I do in openCloseTextFileStream is open exclusively the file and
and attempt to write the bytes back into the return stream then close
the actual fileStream/file - but :
I must provide an initial value in the function for my 'out' value,
mystream .
But if I do this ( within the openCloseTextFileStream function ) :
mystream = null; // so I can complile
Then I get Object reference not set to an instance of an object when
I attempt to fill it with Data.

What is a good way to do this ? Thanks Andrew

I don't quite understand why you're confused. You're effectively
setting findata to null, and then being surprised when you're getting
an exception when you try to dereference it.

Why aren't you setting it to be an *actual* FileStream rather than
null?
 
You are right, it seems setting it to null does not allow me to use it
But the real problem is : how do I pass in the FileStrea
( or can I ?? ) and open a file, load the the FileStream and close the file I opened & return the FileStrea

I'd like the FileStream to be similar to like a memory stream - maybe I just cant do it. What do you think
( I need the FileStream because of the parameters for DataSet.ReadXml method ). Thank

Andrew
 
andrewcw said:
You are right, it seems setting it to null does not allow me to use it.
But the real problem is : how do I pass in the FileStream
( or can I ?? ) and open a file, load the the FileStream and close
the file I opened & return the FileStream

I think you've misunderstood the point of FileStreams. They don't
"contain" the data - they provide access to it, that's all. (They might
have some buffering capacity, but that's incidental.)
I'd like the FileStream to be similar to like a memory stream - maybe
I just cant do it. What do you think ?
( I need the FileStream because of the parameters for DataSet.ReadXml
method ).

Why don't you just open the FileStream and pass that to DataSet.ReadXml
without "loading" it yourself? DataSet.ReadXml will then read the data
from it.
 
I attempted to isolate the reading of the file info out - that's why the memory -esp. since these files are off and on locked by other processes. ( read wait then finally five up ). Thanks anyway
 
Hello Andrew,

I was reviewing this thread. Jon's reply is quite helpful. Do you have any more concerns on it? If there is any questions, please feel free to post
here.

Thanks very much.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
andrewcw said:
I attempted to isolate the reading of the file info out - that's why
the memory -esp. since these files are off and on locked by other
processes. ( read wait then finally five up ). Thanks anyway

In that case, read the file and copy it into a MemoryStream, and then
pass the MemoryStream into ReadXml after setting its position back to
0.

See http://www.pobox.com/~skeet/csharp/readbinary.html for some code to
read the whole of a file into a MemoryStream. (Cannibalise the code
which returns a byte[].)
 
Back
Top