|
Yes, we can use ReadBytes, then why bother to use BinaryReader!?
I like to BinaryReadar because you read primitive types.
using ReadBytes you have to write code like this:
byte[] data = f.ReadBytes(sizeof(double));
double value = BitConverter.ToDouble(data, 0);
The problem is that accroding to documentation:
==== Microsoft Documentation ====
Return Value
A byte array containing data read from the underlying stream. This might be
less than the number of bytes requested if the end of the stream is reached.
======
What happen when EOF is reached? data is null?
Also this could generate an exception to be thrown when I call BitConverter.ToDouble().
BinaryReader is bad designed.
Eduardo Diaz
site | english blog | spanish blog
|
|
|
|
|
ediazc wrote:
What happen when EOF is reached? data is null?
No. You simply get an array that contains less bytes that you requested.
The way to detect end of stream is to examine the length of the array. If the length is shorter than the number of bytes requested, you have reached the end of the stream. The array will contain the last remaining bytes of the stream. If you already read all data up to the end, the length of the array will be zero.
You have to check the length of the array before you try to convert the bytes to a double. If there aren't bytes enough for a double, you have reached the end of the stream.
ediazc wrote:
BinaryReader is bad designed.
I don't think so. Perhaps it's not designed the way you are used to, or maybe not designed the way you try to use it. I believe it's rather well designed for it's purpose, though.
---
b { font-weight: normal; }
|
|
|
|
|
From your blog, your first snippet of code does work, with a few modifications.
void Open()
{
FileStream stream = File.Open(@"C:\myfile.txt", FileMode.Open);
byte b;
while ((b = (byte)stream.ReadByte()) != 255)
{
Console.WriteLine(b.ToString());
}
}
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Then it can't be used to read a file thas has the byte value 255 in it, as that also will be interpreted as end of file.
---
b { font-weight: normal; }
|
|
|
|
|
Oops. b must have been declared int and I shouldn't have casted ReadByte . This will work
void Open()
{
FileStream stream = File.Open(@"C:\myfile.txt", FileMode.Open);
int x;
while ((x = stream.ReadByte()) != -1)
{
byte b = (byte)x;
Console.WriteLine(b.ToString());
}
}
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
|
That particular code is not mine, is taken from microsoft documentation.
The error is from the documenter, who obviusly doesn't know how to read a stream .
Eduardo Diaz
site | english blog | spanish blog
|
|
|
|
|
well.. why don't you just check if the stream position property equals to the length?
that should solve the problem.
void Open()
{
FileStream stream = File.Open(@"C:\myfile.txt", FileMode.Open);
byte b;
while (stream.Position < stream.Length)
{
(b = (byte)stream.ReadByte()) != 255
Console.WriteLine(b.ToString());
}
}
It will also work if you are using a BinaryReader.
|
|
|
|
|
I want my program downloads an HTML file from a site using a simple address that normaly causes my browser to redirect to a LONG complicated address showing the site im after.
But when i try to access the same page from my program using the same shortened address i download an HTML file but its just basicly a blank html file (head and body) with an error message in the body.
Rather than the page my browser redirects to.
I have tried feeding my program the LONG(actual) address and it reads the file just fine form there. But i cant depend on my program recieving the long direct address when its up and running.
So is there a way to get it to read the page it should be redirected to?
Thanks.
-- modified at 21:30 Wednesday 5th October, 2005
/EDIT: I meant to add im using request.AllowAutoRedirect=true;
|
|
|
|
|
Hmm it seems the redirection does work when used on other pages.... is it possible that some form of spider/bot blocking could be inplace to stop the redireciton or is it somthing as simple as me needing to learn how to enable cookies, etc?
|
|
|
|
|
Hi,
I am doing a project using forms. I want to create a global struct common to all forms. I want to create an array of that struct and to set the values in one form and retrieve in other forms.
How do I do it. I have created the struct but each time when I create an object for the struct the values are lost.
pls help.
|
|
|
|
|
hi there,
Sure everytime u create an obj for the struct, actually it's a new obj so no old values are stored in this obj.
ter81 wrote:
I want to create a global struct common to all fo
just create this in the top parent form's scope so that u can access this later in another forms. And this will be freed from memory when the top form is closed.
Hope this answers
<< >>
|
|
|
|
|
I dont have a parent-child form hierarchy, but individual form that have links to each other.
I have created the structure iinside the namespace. but I was wondering how do I create a global object for this structure.
|
|
|
|
|
You can do whatever u want in ur app. But whenever ur app is still alive, the pointer to the struct should be persistent too.
U can achieve this by using static variable.
<< >>
|
|
|
|
|
There is no such thing as global in C#. Instead, create the array as static within a class you use for that purpose, then you can access it from anywhere.
If it's being used only in one class, then just make it static in that class. Static means that only one exists, no matter how many class instances exist.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
How can I get the currency symbol from just the ISO 4217 standard 3-letter code.
CAD = $
USD = $
GBP = £
YEN = ¥
I tried creating a RegionInfo by stripping out the first two characters of the 3-letter code but is doesn't work for all cases.
Any ideas?
|
|
|
|
|
I have writen a program that extracts data from a source that can be highly ambiguous with its formatting of address lines but the Postcode is always(99.9%) correclty formated. The database my program need to export this compiled data to requires very specific address formatting.
So i was wondering what was the best way look up addresses from UK postcodes in a way that will spit out an address in a consitantly formatted manner so they might be more easily translated into my dataset.
Web services, third party programs, anything?
The free'er the better as this is for some volunteer work I'm undertaking and the budget is ... well.... next to nothing.
Cheers.
|
|
|
|
|
Dear all,
To get familiar with the C# language I have started a project involving Forms, Controls and GDI+. The aim is a sort of curve editor (don't ask me why)
Please have a look at the following screenshot:
http://www.redtricycle.net/code/gamma_curve_unpainted.png[^]
The structure of the program is as follows:
- The window is a standard Windows Form.
- The khaki rectangular area is an inherited Panel which implements a Paint event handler to draw the grid and the curve.
- The point handles inherit from Control objects (which easily facilitate the handling of mouse events). These controls are added to the Panel.
As you might notice, there is this rectangle of 200x100 that won't be painted. Only the controls inside the panel are painted but not the stuff drawn by the Panels Paint event handler.
Since the default size of a panel is 200x100 I've overridden the DefaultSize property and set it to 0,0 but this did not have any effect.
Do any of you have an idea of what could be the problem here? I'm a little puzzled by it.
Thanks for your response in advance!
For those that want to get a feel of the program, the exe is here:
http://www.redtricycle.net/code/GammaCurve2.zip[^]
Red Tricycle
|
|
|
|
|
You need to clip the Panel by either setting its Region property to just the part you draw, or in your OnPaint override (or handling the Paint event, although deriving your own class fosters better reuse of code) or set the Graphics.Clip property.
I do recommend considering not using controls such as Panel for the various drawing elements. Use polymorphism to define a base class for graphical objects that derivative classes override the painting. The panel would simply host a collection of the base class and call the virtual painting method. This requires far fewer system resources because controls are reather heavy-weight objects and you will notice as you get more controls hosted in your process.
There are lots of articles here on CodeProject about supporting this polymorphic model.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thanks for the reply and for giving an interesting insight in how to set things up in a light-weight fashion. I'll investigate this option for sure but first I'll try the Region/Clip options.
|
|
|
|
|
Hi!
I'm implement my own calendar class derived from System.Globalization.Calendar.
I need to make my calender with more that 12 months in a year (say 16). But then I use the DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar), it givs me a ArgumentOutOfRangeException. The parameters states that month is 1 through the number of months in calendar and day is 1 through the number of days in month. There is the problem how do I define my own calendar class so I can set ex. DateTime(1800, 15, 10). I thing that DateTime use the abstract mathod ToDateTime(...) to deliver a instance.
May help:
http://www.abgeen.com/JalaliCalendar.cs[^]
http://www.cs.oberlin.edu/~jwalker/calendarReform/GlobalCalendar.cs[^]
Best regards, Gywox
|
|
|
|
|
What's the best way to validate a filename?
In my UI, the user must enter a filename in a Forms.TextBox control.
This would be for a new file for saving data, so checking if it exists is of no use.
I can override the OnValidating event, but what chars do I look for?
Kathy.
|
|
|
|
|
The Path object has an InvalidPathChars property. Look for that in the user-entered string like so:
if(myPath.IndexOfAny(System.IO.Path.InvalidPathChars) == -1)
{
}
Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..."
-Jörgen Sigvardsson
|
|
|
|
|
Hi all!
I am writing an windows application using MySQL as the source database. Can anybody tell me how to get the datarow when I select some specified rows from the database please? I try to use the following way to do that, but it doesn't work:
<br />
string commandtext = "select * from machine where price = 100;";<br />
OdbcCommand command = new OdbcCommand(commandtext, connection);<br />
OdbcDataReader reader = command.ExecuteReader();<br />
ArrayList row = new ArrayList();<br />
int i = 0;<br />
while (reader.Read())<br />
{<br />
row.Add((DataRow) reader.GetValue(i++);<br />
}<br />
reader.Close();<br />
Could anybody tell me how to solve this please!!!
Thanks in advance!!!
Asura
|
|
|
|
|
OdbcDataReader.GetValue gets the column specified by the ordinal you supply. So reader.GetValue(i++) only returns one column and if you have multiple rows (more rows than you have columns) you will probably find that your code throws an exception once i is greater than the number of columns in your database.
If you want all the columns you need to use GetValues which will fill an array with all the column values you have.
The easier way to do this though is to use a DataTable.
Also, if you're using MySQL you could consider downloading and using the MySQL Connector for .NET (http://dev.mysql.com/downloads/connector/net/1.0.html[^])
|
|
|
|