473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

what is the meaning of .close in binaryreader

14
I have binary file of integers and I use some C# code to read from it.

I am a complete beginner in C#(I know better C)

looking at some examples I see that .Close() is used in the end for a binaryreader(never used binaryreader in C)

but if I try to use it then it cause errors if I use the same function twice.

It seems better simply not to close binaryreader and I wonder what is the advantage of closing it

Here is a simple example for a code that works only after I commented r.close out of the program
You can see that I never close r in the code and I wonder if this type of code
can cause problems.


Expand|Select|Wrap|Line Numbers
  1.   using System;
  2. using System.IO;
  3. // Program start class
  4.  
  5. class InteractiveWelcome 
  6. {
  7.  
  8.     private const string FILE_NAME = "outputfile.bin";
  9.     public int find_number(int place, FileStream fs)
  10.     {
  11.         int num;
  12.         BinaryReader r = new BinaryReader(fs);
  13.         fs.Seek(place * sizeof(Int32), SeekOrigin.Begin);
  14.         num = r.ReadInt32();
  15.   //      r.Close();
  16.         return num; 
  17.     }
  18.  
  19.     public static void Main(String[] args)
  20.     {
  21.         InteractiveWelcome fd=new InteractiveWelcome();
  22.         FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
  23.         Console.WriteLine(fd.find_number(0,fs));
  24.         Console.WriteLine(fd.find_number(1,fs));
  25.         fs.Close();
  26.     }
  27.  
  28. }
Jan 20 '10 #1
3 1976
Plater
7,872 Expert 4TB
.Close() does just what it sounds like, it closes the stream, releasing the memory it used (and also probably the file it was reading from).
You would call it when you are done with the object
Jan 20 '10 #2
urib
14
Thanks
I guess that closing the file is the reason for the error if I use r.Close()

In this case the question is if closing the file by fs.Close() when I finish the program is enough and I do not need to close the binary reader..
Jan 20 '10 #3
Plater
7,872 Expert 4TB
I would say yes, fs.close() is enough.
the binary read would just call it internally anyway.
Jan 20 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Chris P. | last post by:
I have a C# application that connects to Perl application on a UNIX server. I am able to connect and communicate both directions with the server, but there are several occasions when it appears...
14
by: Laszlo Szijarto | last post by:
Can BinaryReader be forced to read a stream, say a TCP/IP stream or memory stream or even file stream in big endian order or do I have to write something custom to reverse the byte order? So, for...
7
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the...
2
by: Bob Rock | last post by:
I already found an alternative way to accomplish this (using ReadBytes), still I'd like to understand why I'm getting and error reading a text file using the following method. The exception is...
19
by: Mikolas | last post by:
I was trying to use BinaryReader.PeekChar to "peek" the next byte, that works fine unless the byte value's above 127 .... is there anything I can do abouit it? thnx, mikolas
6
by: Question with BinaryReader | last post by:
I use BinaryReader to read my binary dafa files, when i call ReadBytes, why it always return more 4 bytes. The following is my code. FileStream fs = new FileStream(file, FileMode.OpenOrCreate,...
1
by: Vitaly | last post by:
// Open input file and create the BinaryReader. br = new BinaryReader(new FileStream("Test.dat", FileMode.Open, FileAccess.Read)); // Read binary data. d = br.ReadDouble(); A question is...
2
by: I_AM_DON_AND_YOU? | last post by:
Can BinaryReader Object read the Word file?
8
by: ForrestPhoto | last post by:
Hi, I must be missing something stupid. This works fine for text files, but uploads about half of images ( jpg & png ) before cutting out, and leaving a useless file on the server. It doesn't...
7
by: Peter Morris | last post by:
Can I use a BinaryWriter without it closing the underlying stream? I want to save information to a MemoryStream using a BinaryWriter but I want to dispose of the BinaryWriter and keep the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.