473,322 Members | 1,523 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,322 software developers and data experts.

Reading File Contents using a struct

I have a file that I want to read. The structure is given in C++

eg.
Expand|Select|Wrap|Line Numbers
  1. typedef HEADER_TAG
  2. {
  3.     UCHAR index[2];
  4.     UCHAR crt[4];
  5. }HEADER_TAG
  6.  
How do I get the data out of the file in C# using the above structure? What is an equivalent of this structure in C#?

Do I need to read in byte by byte and pass a struct? How?

Can anyone help?
Dec 26 '07 #1
6 1225
CyberSoftHari
487 Expert 256MB
Structure never read a file. We have streams to read files in C# (Assumed .txt file).
Note: You have to mention your frontend and what type of file you want to read.
Dec 26 '07 #2
Plater
7,872 Expert 4TB
In c++ an array of bytes could be type-casted as a struct.
(If the struct contained two 16bit integers, a 4 byte array could be cast as the struct)

In C# the dealins with structs are much trickier. I have taken to just creating a class with the same members as the struct, and mandating that the constructor take in an array of bytes. It would then populate the member variables based on that byte[].

I believe serialization is designed to this for us, but I've not used it.
Dec 26 '07 #3
Structure never read a file. We have streams to read files in C# (Assumed .txt file).
Note: You have to mention your frontend and what type of file you want to read.
Thanks for your reply.

I meant a custom file type (eg ".rpa"). I can use streams to get out the file

eg..
Expand|Select|Wrap|Line Numbers
  1. FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read);
  2. byte [] data = new byte[file.Length];
  3. file.read(data, 0, data.Length);
  4. file.close();
Now, if I have data {dimension = 32}, how do I get the specific data in string format given the structure above?
Dec 31 '07 #4
CyberSoftHari
487 Expert 256MB
Go through below link and solve your assignment,
FileStream Constructor (String, FileMode, FileAccess, FileShare)
Note: Asking homework is prohibited you have to try your self.
Dec 31 '07 #5
Frinavale
9,735 Expert Mod 8TB
Hi there!
I think you should consider Serializing your Class.
When you need to save the data to a file, serialize the instance of that object.

There's many ways to Serialize an object...some of them include: XML Serialization and Binary Serialization...there are other types as well. You should consider researching the types available to you and pick the one that best suits your application's specific needs.

I think this will help you to extract the data that you need.

-Frinny
Dec 31 '07 #6
Hi there!
I think you should consider Serializing your Class.
When you need to save the data to a file, serialize the instance of that object.

There's many ways to Serialize an object...some of them include: XML Serialization and Binary Serialization...there are other types as well. You should consider researching the types available to you and pick the one that best suits your application's specific needs.

I think this will help you to extract the data that you need.

-Frinny
Thanks a bunch! This actually helped....
Jan 2 '08 #7

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

Similar topics

3
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be...
20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
0
by: sangui | last post by:
Hello. this is beginner programmer. If u have more time, would you check this code.? please give me the answer how to slove this problem...? ÷ºÎ ÆÄÀÏ ==> wtmpx.dat ·Î±× ÆÄÀÏ ...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
16
by: Jm.GlezdeRueda | last post by:
Hi all, Im trying to read a 24bit bmp with fread, and i have some problems.. I want to read the whole structure in one time, but i dont know why, it only reads the first member well.. I have...
9
by: Eric Lilja | last post by:
Hi! I have a program with a class that needs to be able to write itself to a file in clear text format. The file has two integers and vector of struct objects. The struct has a string that can...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.