473,938 Members | 16,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Closing a stream(easy question)

Hi,
simple question

XmlDocument doc = new XmlDocument();
FileStream file = new FileStream
(PathAndFileNam e,FileMode.Open ,FileAccess.Rea d,FileShare.Rea dWrite);
doc.Load(file);

should I now call file.close or do I not need to??

Thanks
Nick
Nov 16 '05 #1
6 7528
Enclose the doc.Load(...) statement in the try-finally block and add the
following in the "finally" clause:

try
{
file.Close();
}
catch(IOExcepti on)
{
// Optionally add something like this here:
// Debug.WriteLine ("Failed to close the file");
}

"Nick" <ni*********@ho tmail.com> wrote in message
news:a0******** *************** ***@posting.goo gle.com...
Hi,
simple question

XmlDocument doc = new XmlDocument();
FileStream file = new FileStream
(PathAndFileNam e,FileMode.Open ,FileAccess.Rea d,FileShare.Rea dWrite);
doc.Load(file);

should I now call file.close or do I not need to??

Thanks
Nick


Nov 16 '05 #2
> should I now call file.close or do I not need to??

yes you should call file.Close(). the whole stream is read in the Load()
method and it's not necessary to leave the file opened.

Chris
Nov 16 '05 #3
Hi Nick,

Yes, you should call file.Close(); XmlDocument.Loa d will not close the stream itself. In any case, calling file.Close() on an already closed file will just be ignored and will do no harm.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4
> Yes, you should call file.Close(); XmlDocument.Loa d will not close the
stream itself. In any case, calling file.Close() on an already closed file will just be ignored and will do no harm.


after calling close on a FileStream, it will likely become null. the
Close() method of FileStream calls Dispose(). I haven't tested it, but I'm
fairly sure it will be null in all cases.

Chris
Nov 16 '05 #5
Calling Close() will release any resources tied to it, in this case the file. The FileStream object will remain intact however, so you can safely call Close() as many times as you want.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #6
Chris <ct******@hotma il.com> wrote:
Yes, you should call file.Close(); XmlDocument.Loa d will not close the
stream itself. In any case, calling file.Close() on an already closed file will
just be ignored and will do no harm.


after calling close on a FileStream, it will likely become null.


No it won't. Objects don't just disappear because you call Close on
them. If you still have a reference to an object, that reference isn't
going to become null on its own.
the Close() method of FileStream calls Dispose(). I haven't tested it, but I'm
fairly sure it will be null in all cases.


Could you clarify exactly what you think "will be null"?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2033
by: Sonny | last post by:
Hi experts, I am writing a small code to read input from a file, for example my input file is: X Y Z 26.0 28.0 0.0 32.0 8.0 0.0 My code is: #include<iostream>
5
2187
by: Riku Jarvinen | last post by:
Hi everyone, I asked another question regarding this same subject about a week ago. See thread: http://groups.google.fi/group/comp.lang.c++/browse_frm/thread/cc57b579d6832732/530bae667a479add#530bae667a479add So, the basic idea is to have a compact logger class which can be used like the std::cout stream and add extra functionality to it (line numbering, some output counters etc.).
3
2737
by: Grant Harmeyer | last post by:
I have an XSL file that is being applied to an RSS feed, and it works great except for 2 things: 1.) XSL doesn't close the <img> tag for xhtml even though I have it being closed in the XSL file (I've tried <img /> and <img></img>). This blows my xhtml compatibility. 2.) Also, the RSS feed has a namespace "dc", how do I extract data from the elements have a dc namespace? I've tried: <div class="bodysm"><i><xsl:value-of...
5
1187
by: John Salerno | last post by:
Is this valid: string allSwitchValues = readSwitches.ReadToEnd().Split('|'); I have no way to test it just yet (although it didn't seem to create compile errors). Originally I had it divided into two variables on two lines, but it seems like I only need the one variable, assuming I can't combine the ReadToEnd and Split methods like so.
2
996
by: **Developer** | last post by:
I have a UserControl on a form. The form is closing and I need the usercontrol to write to the Registry. Where do I do that? In the Disposed event? I need to do it when controls on the usercontrol are still active so I can write, for example, the Text value of a Textbox. In the Overrides Sub Dispose method? if so, before or after the If disposing Then statement?
4
1459
by: Paul Hemans | last post by:
Hi newbie here. I am learning c++/cli I by writing console apps. When my apps finished they used to display a message at the bottom of the screen, something like "press any key to continue" so that you could see the results before returning to the IDE. But now they don't. I must have turned a setting off somewhere but I can't find it. I could code it in but I would prefer not to. Any ideas?
2
1273
by: john | last post by:
In TC++PL 3, on page 616, "class basic_ios" has the public members "operator void *() const; // nonzero if !fail() bool operator!() const { return fail(); }" for using them in conditional statements in the style: if(cin) // if( !void *(0) ) if(!cin) // if( cin.operator!()
5
7271
by: TBass | last post by:
Hi, I'm moving a socket library I wrote from C to C++. In the C version, I had to malloc char arrays to store incoming communication. My hope was to use std::string in C++, but then I realized a problem. While '\0' is a valid string terminator for text, for my purposes it is a problem. My program regularly gets '\0' as a value (Modbus/RTU and TCP pass register values as the actual values, not the ASCII chart). That would be a problem...
6
1172
by: klaas | last post by:
Hi, i'am writing a program in vb.net 2005 based on an access database. Now, i want to close my program automaticly when the user is inactive to a period of eg 10 minutes. I want to close all mine connections to the database en quit the program. is there a procedure to achive this or even better an example of this on the net? regards, Klaas
0
10125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9963
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11509
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11281
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8207
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7377
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6072
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4441
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.