Can this be done using C#

P

Pete Kane

Hi All, I'm in the process of rewriting some very old C ( but still
useful ) database utilities and need some help with the type of "read"
function used in many of them. Is there an equivalent "read" function in
C# ? If I'm reading this correctly ( pun intended !) this read function
reads a numer of bytes directly into a struct

Regards




#include "stdafx.h"
#include "dbfhead.h"
#include "stdio.h"
#include "stdlib.h"
#include "fcntl.h"
#include "io.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

typedef struct
{
char dbf_id;
char last_update[3];
long last_rec;
short data_offset;
short rec_size;
char filler[20];
} DBF_HEAD;


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
int handle;
DBF_HEAD dbf_head;


if(argv[1] == NULL)
{
printf("Usage dbfhead dbname, e.g. dbfhead c:\\data\\datfile.dbf\n");
exit(-1);
}

handle = open(argv[1],O_RDONLY|O_RAW);

if(read(handle,(char*)&dbf_head,sizeof(dbf_head)) !=sizeof(DBF_HEAD))
{
printf("\nRead Error\n");
exit(1);
}


printf("\n*** Database Header %s ***\n\n",argv[1]);
printf("\nDbf Id %d\n",dbf_head.dbf_id);
printf("Last Update %d %d
%ld\n\n",dbf_head.last_update[2],dbf_head.last_update[1],dbf_head.last_update[0]);
printf("Data Offset %d\n",dbf_head.data_offset);
printf("Record Size %d\n",dbf_head.rec_size);
printf("Number of Records %ld\n",dbf_head.last_rec);
close(handle);
return nRetCode;
}
 
A

Andreas Johansson

You should have a look at the classes in System.IO namespace.

The FileStream class and File.OpenRead method are likely to fit your needs.

FileStream fs = File.OpenRead(args[0]);
 
A

Andreas Johansson

You should have a look at the classes in System.IO namespace.

The FileStream class and File.OpenRead method are likely to fit your needs.

FileStream fs = File.OpenRead(args[0]);
 
P

Pete Kane

Andreas said:
You should have a look at the classes in System.IO namespace.

The FileStream class and File.OpenRead method are likely to fit your needs.

FileStream fs = File.OpenRead(args[0]);
Hi Andreas, thanks, yes I know how to open and read files , but, how can
I read some data into a struct ? - the rest of the program is not a problem

Regards
 
P

Pete Kane

Andreas said:
You should have a look at the classes in System.IO namespace.

The FileStream class and File.OpenRead method are likely to fit your needs.

FileStream fs = File.OpenRead(args[0]);
Hi Andreas, thanks, yes I know how to open and read files , but, how can
I read some data into a struct ? - the rest of the program is not a problem

Regards
 
M

Markus Erlacher

Hi Andreas, thanks, yes I know how to open and read files , but, how can
I read some data into a struct ? - the rest of the program is not a problem

If you don't have an ExpertsExchage account, here is what I just did:

GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
InternalDataParcel idp = (InternalDataParcel)Marshal.PtrToStructure
(handle.AddrOfPinnedObject(), typeof(InternalDataParcel));
handle.Free();

where InternalDataParcel is the struct in my case and buffer a byte
array that I read from a file. But you have to make sure that the byte
alignment of the struct in C and C# are alike. I ran into problems
with bool values in my case, so I used integers only instead.

Markus
 
M

Markus Erlacher

Hi Andreas, thanks, yes I know how to open and read files , but, how can
I read some data into a struct ? - the rest of the program is not a problem

If you don't have an ExpertsExchage account, here is what I just did:

GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
InternalDataParcel idp = (InternalDataParcel)Marshal.PtrToStructure
(handle.AddrOfPinnedObject(), typeof(InternalDataParcel));
handle.Free();

where InternalDataParcel is the struct in my case and buffer a byte
array that I read from a file. But you have to make sure that the byte
alignment of the struct in C and C# are alike. I ran into problems
with bool values in my case, so I used integers only instead.

Markus
 
P

Pete Kane

Markus said:
If you don't have an ExpertsExchage account, here is what I just did:

GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
InternalDataParcel idp = (InternalDataParcel)Marshal.PtrToStructure
(handle.AddrOfPinnedObject(), typeof(InternalDataParcel));
handle.Free();

where InternalDataParcel is the struct in my case and buffer a byte
array that I read from a file. But you have to make sure that the byte
alignment of the struct in C and C# are alike. I ran into problems
with bool values in my case, so I used integers only instead.

Markus
Thanks Markus - looks promising
 
P

Pete Kane

Markus said:
If you don't have an ExpertsExchage account, here is what I just did:

GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
InternalDataParcel idp = (InternalDataParcel)Marshal.PtrToStructure
(handle.AddrOfPinnedObject(), typeof(InternalDataParcel));
handle.Free();

where InternalDataParcel is the struct in my case and buffer a byte
array that I read from a file. But you have to make sure that the byte
alignment of the struct in C and C# are alike. I ran into problems
with bool values in my case, so I used integers only instead.

Markus
Thanks Markus - looks promising
 
P

Pavel Minaev

Thanks, I'm not a member of that group so can't see any replies

You don't need to be a member of Experts Exchange to read the replies
(despite what their page says). Just scroll down past all the "All
comments and solutions are available to Premium Service Members"
placeholders, and past all the ads that follow, and you'll see the
real comments there.

The reason is quite ironic. EE wants to be indexed by Google (so they
come up on top or close in Google searches for relevant key phrases).
However, Google TOS requires that websites do not discriminate between
Google bot and normal visitors - in other words, it can't show all
comments to the bot (so they get indexed and appear in search
results), but deny access to that very same indexed content to users
that come there by clicking on those search results. So EE uses this
deceiving practice of pretending that things are hidden when in
practice they aren't, in hope you just believe them.
 
P

Pavel Minaev

Thanks, I'm not a member of that group so can't see any replies

You don't need to be a member of Experts Exchange to read the replies
(despite what their page says). Just scroll down past all the "All
comments and solutions are available to Premium Service Members"
placeholders, and past all the ads that follow, and you'll see the
real comments there.

The reason is quite ironic. EE wants to be indexed by Google (so they
come up on top or close in Google searches for relevant key phrases).
However, Google TOS requires that websites do not discriminate between
Google bot and normal visitors - in other words, it can't show all
comments to the bot (so they get indexed and appear in search
results), but deny access to that very same indexed content to users
that come there by clicking on those search results. So EE uses this
deceiving practice of pretending that things are hidden when in
practice they aren't, in hope you just believe them.
 
B

Ben Voigt [C++ MVP]

Pavel said:
You don't need to be a member of Experts Exchange to read the replies
(despite what their page says). Just scroll down past all the "All
comments and solutions are available to Premium Service Members"
placeholders, and past all the ads that follow, and you'll see the
real comments there.

The reason is quite ironic. EE wants to be indexed by Google (so they
come up on top or close in Google searches for relevant key phrases).
However, Google TOS requires that websites do not discriminate between
Google bot and normal visitors - in other words, it can't show all
comments to the bot (so they get indexed and appear in search
results), but deny access to that very same indexed content to users
that come there by clicking on those search results. So EE uses this
deceiving practice of pretending that things are hidden when in
practice they aren't, in hope you just believe them.

What browser are you using? I always have to login with my account (no cost
to sign up, I don't know if that's changed) in order to see solutions. So
it's totally free but you do have to log in.
 
B

Ben Voigt [C++ MVP]

Pavel said:
You don't need to be a member of Experts Exchange to read the replies
(despite what their page says). Just scroll down past all the "All
comments and solutions are available to Premium Service Members"
placeholders, and past all the ads that follow, and you'll see the
real comments there.

The reason is quite ironic. EE wants to be indexed by Google (so they
come up on top or close in Google searches for relevant key phrases).
However, Google TOS requires that websites do not discriminate between
Google bot and normal visitors - in other words, it can't show all
comments to the bot (so they get indexed and appear in search
results), but deny access to that very same indexed content to users
that come there by clicking on those search results. So EE uses this
deceiving practice of pretending that things are hidden when in
practice they aren't, in hope you just believe them.

What browser are you using? I always have to login with my account (no cost
to sign up, I don't know if that's changed) in order to see solutions. So
it's totally free but you do have to log in.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top