Using fread in c#

Started by
7 comments, last by Mike.Popoloski 14 years, 1 month ago
When i was using fread to read files, i discovered that there's one really weird problem. To make sure what was going on i made a file that has bytes from 0 to 255 written inside and this is how i read it: IntPtr P = IntPtr.Zero; P = BinFile.fopen(@"C:\bleh.txt", "r"); byte b = 0; for (int i = 0; i < 255; i++) { b = 0; BinFile.fread(&b, sizeof(byte), 1, P); Console.WriteLine(b); } BinFile.fclose(P); Also this is how i use fopen and fread: [DllImport("MSVCRT.DLL")] public static extern IntPtr fopen(string Path, string Mode); [DllImport("MSVCRT.DLL")] public static extern IntPtr fread(void* Buffer, uint Size, uint Count, IntPtr Stream); Well anyways... when i was reading it and it was about to read byte with the value of 26, it fails to read it and it fails with the next ones too. It seems to fail every time there's byte 26 to read. Does anybody have any idea what's going on and how to make read it right?
Advertisement
Did you try opening the file as binary?
You do realize that there is an entire System.IO namespace right?
Mike Popoloski | Journal | SlimDX
Why on earth are you using fread?
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Because i can use fread to read a whole structure out of the file in one code line.
You do realize that there is an entire System.Runtime.Serialization namespace right?
Mike Popoloski | Journal | SlimDX
I just realized that before you said it by the way.
And way to go solving the problem.
Google and "xmlserializer c#" => this.
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]
Quote:Original post by tanel1993
I just realized that before you said it by the way.
And way to go solving the problem.


The problem is that you're using an old and decrepit C API in a modern managed language. I've pointed you towards a correct solution to your real problem, even if you can't realize it. Trying to get fread to work will just be a waste of your time.
Mike Popoloski | Journal | SlimDX

This topic is closed to new replies.

Advertisement