473,890 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading Random Access file

3 New Member
Hi All,
I have a Random access file which is written using VB 6.0.
I need to read this file using C#.
The record used in VB to write the Random access file is as follows

Type AA
aa1 As Integer
aa2 As Integer
aa3 As Integer
aa4 As Integer
aa5 As Integer
aa6 As Integer
aa7 As Integer
aa8 As Integer
aa9 As Integer
aa10 As String * 10
aa11 As String * 20
aa12 As String * 20
aa13 As String * 20
aa14 As String * 20
aa15(1 To 20) As String * 150
aa16(1 To 40) As Integer
aa17(1 To 3) As String * 20
aa18 As Byte
aa19 As String * 1
aa20 As Byte
aa21 As Byte
aa22 As Byte
aa23 As Byte
aa24 As Byte
aa25 As Byte
aa26 As Byte
aa27 As Byte
aa28 As Byte
aa29l(1 To 20) As Double
aa30(1 To 20) As Single
aa31 As Byte
aa32 As String * 20
aa33 As Byte
aa34 As Byte
aa35 As Byte
aa36 As Integer
aa37 As Double
aa38 As String * 25
aa39 As String * 25
aa40 As String * 25
aa41 As Byte
aa42 As Integer
aa43 As Integer
aa44 As String * 10
aa45 As String * 15
End Type

This is the structure that i have created in C# to match the record in VB


[StructLayout(La youtKind.Sequen tial,Pack =1,CharSet = CharSet.Ansi)]
public struct AAStructure{
public short aa1;
public short aa2;
public short aa3;
public short aa4;
public short aa5;
public short aa6;
public short aa7;
public short aa8;
public short aa9;
[MarshalAs(Unman agedType.ByValA rray,SizeConst =10)]
public char[] aa10;
[MarshalAs(Unman agedType.ByValA rray,SizeConst =20)]
public char[] aa11;
[MarshalAs(Unman agedType.ByValA rray,SizeConst =20)]
public char[] aa12;
[MarshalAs(Unman agedType.ByValA rray,SizeConst =20)]
public char[] aa13;
[MarshalAs(Unman agedType.ByValA rray,SizeConst =20)]
public char[] aa14;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 20)]
public String150[] aa15;

[MarshalAs(Unman agedType.ByValA rray, SizeConst = 40,ArraySubType =

UnmanagedType.I 2)]
public short[] aa16;

[MarshalAs(Unman agedType.ByValA rray, SizeConst = 3)]
public String20[] aa17;
public byte aa18;
public char aa19;
public byte aa20;
public byte aa21;
public byte aa22;
public byte aa23;
public byte aa24;
public byte aa25;
public byte aa26;
public byte aa27;
public byte aa28;

[MarshalAs(Unman agedType.ByValA rray, SizeConst = 20, ArraySubType =

UnmanagedType.I 4)]
public int[] aa29;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 20, ArraySubType =

UnmanagedType.R 4)]
public Single[] aa30;
public byte aa31;
[MarshalAs(Unman agedType.ByValA rray,SizeConst =20)]
public char[] aa32;
public byte aa33;
public byte aa34;
public byte aa35;
public short aa36;
public int aa37;
[MarshalAs(Unman agedType.ByValA rray,SizeConst =25)]
public char[] aa38;

[MarshalAs(Unman agedType.ByValA rray,SizeConst =25)]
public char[] aa39;

[MarshalAs(Unman agedType.ByValA rray,SizeConst =25)]
public char[] aa40;

public byte aa41;
public short aa42;
public short aa43;

[MarshalAs(Unman agedType.ByValA rray,SizeConst =10)]
public char[] aa44;

[MarshalAs(Unman agedType.ByValA rray,SizeConst =15)]
public char[] aa45;

}

[StructLayout(La youtKind.Sequen tial, Pack = 1, CharSet = CharSet.Ansi)]
public struct String150 {
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 150)]
public char[] value;

}


[StructLayout(La youtKind.Sequen tial, Pack = 1, CharSet = CharSet.Ansi)]
public struct String20 {
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 20)]
public char[] value;

}

however, if i read the random access file using the structure, i am not getting the correct values in the structure data members.

Is my structure correct ?
Am i missing any thing ?

Thanks in advance for any help,

regards,
knuckels23
Nov 8 '08 #1
4 2677
mldisibio
190 Recognized Expert New Member
Are you reading the record using VB6 into a VB6 structure, and then trying to pass that structure to a C# application using COM Interop?

Or are you simply reading the file using C#?

If you are reading the file using C#, you do not need all the COM Interop attributes. C# would not care what application wrote the file and you could read the file and fill the struct using native types and methods.

That said, either way, I see you interpreted all your source integers as C# shorts, which are 16 bit, not 32. Was that your intent? Are the values always less than 32768?

Just to check, are all the source strings fixed length? That is, is a VB source string * 10 guaranteed to occupy 10 characters or blanks in the file, or is there a chance they could be shorter?

Any issues with encoding?

Have a read through Default Marshalling For Strings and Default Marshalling For Arrays

Sometimes StringBuffer and not char[] is needed for the unmanaged code to fill with fixed string data.

Perhaps post a sample snippet of how you are filling the C# structure...ple ase do not post every field: that is overkill when posting for problem solving.

Also, please describe what you mean when you say you are not getting correct values: are they blank, are there exceptions thrown, are they all off by one, are they complete garbage data, etc.

Finally, USE THE "CODE" tags when posting code, out of courtesy to the forum.

Thanks,

Mike
Nov 9 '08 #2
knuckels23
3 New Member
Are you reading the record using VB6 into a VB6 structure, and then trying to pass that structure to a C# application using COM Interop?

Or are you simply reading the file using C#?

If you are reading the file using C#, you do not need all the COM Interop attributes. C# would not care what application wrote the file and you could read the file and fill the struct using native types and methods.

That said, either way, I see you interpreted all your source integers as C# shorts, which are 16 bit, not 32. Was that your intent? Are the values always less than 32768?

Just to check, are all the source strings fixed length? That is, is a VB source string * 10 guaranteed to occupy 10 characters or blanks in the file, or is there a chance they could be shorter?

Any issues with encoding?

Perhaps post a sample snippet of how you are filling the C# structure...ple ase do not post every field: that is overkill when posting for problem solving.

Also, please describe what you mean when you say you are not getting correct values: are they blank, are there exceptions thrown, are they all off by one, are they complete garbage data, etc.

Finally, USE THE "CODE" tags when posting code, out of courtesy to the forum.

Thanks,

Mike
Hi Mike,

Thanks for the reply.
I apologize for posting all the fields and not using the code tags.

1. I am simply reading the file using C# .
2."C# would not care what application wrote the file and you could read the file and fill the struct using native types and methods." - I may be wrong here, but I still have to specify the interop attributes for Array and fixed string's ??
3. " That said, either way, I see you interpreted all your source integers as C# shorts, which are 16 bit, not 32. Was that your intent? Are the values always less than 32768?" - I may be wrong here too, but I think Integers in VB means 2 BYTES, that's why I mapped it to a C# Short.
4. The document based on which i am working states that all the source strings will be of fixed length, how ever i will double check it.
5.The data is not encoded, so i believe there should not be any issues of encoding.
6. I will Post the code that i am using to populate the Structure in C#, how ever before that i need to know whether the my C# structure maps the VB structure.
STRIPED DOWN VB STRUCTURE
Expand|Select|Wrap|Line Numbers
  1. Type AA   aa1 As Integer   aa10 As String * 10       aa15(1 To 20) As String * 150   aa16(1 To 40) As Integer     aa17(1 To 3) As String * 20          aa18 As Byte            aa19 As String * 1        aa29(1 To 20) As Double      aa30(1 To 20) As Single        aa37 As Double       End Type
MATCHING C# STRUCTURE
Expand|Select|Wrap|Line Numbers
  1.         [StructLayout(LayoutKind.Sequential,Pack =1,CharSet = CharSet.Ansi)]               public struct AAStructure{             public short aa1;             [MarshalAs(UnmanagedType.ByValArray,SizeConst =10)]             public char[] aa10;             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]             public String150[] aa15;             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 40,ArraySubType=UnmanagedType.I2)]             public short[] aa16;             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]             public String20[] aa17;             public byte aa18;             public char aa19;             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20, ArraySubType =UnmanagedType.I4)]             public int[] aa29;             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20, ArraySubType =UnmanagedType.R4)]             public Single[]  aa30;             public int aa37;         }          [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]         public struct String150 {             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 150)]             public char[] value;          }          [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]         public struct String20 {             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]             public char[] value;          } 
Can you please check the above code ??

Thanks a ton again for the swift response and thanks in advance again for any further help .
regards,
knuckels23
Nov 9 '08 #3
mldisibio
190 Recognized Expert New Member
If the VB integers are two bytes, then you were correct to use short.

Looking over the two structs, it certainly looks correct to me, but of course one would have to test and verify. Not sure about 29 and 37 where you call the VB Double a C# int, but I assume you determined it was the correct match.

OK, I may be missing something, but if indeed you are simply reading a file, be it binary or ascii, using C#, you do not need the Interop attributes.

Interop means a .net (managed) application is talking directly to a non-.NET (unmanaged) application and exchanging data via structures, such as you created.

Although the VB app wrote the data to a file where each record represents the VB type you posted, ONCE IT IS WRITTEN the file has no concept of those structures. It is simply character or binary data.

Now, in order to read it, the .Net IO classes only see a file with a series of bytes. IF the content is text, the TextReaders need to know the encoding and what the EndOfLine character is, and they are happily off reading it.

If the content is binary, which is probably your case, then yes, the BinaryReader has read methods which need to know the data type it is reading. For example: ReadInt16() will read two bytes from the stream and output a short which you can than store or use however you want.

However, it does not need to know about String150[] for example, or any other types beyond primitive. If there are 20 strings each 150 characters, then you can either call ReadChars(150) 20 times, or even ReadChars(3000) . Once you have that data, then yes, you can store it in a struct like the one you created in order to emulate the VB source. But the binary reader only needs to know primitive types.

Finally, even if you do create your own struct and fill it using the BinaryReader, as long as you are not trying to send it back to the VB application, or get it from the VB application, THERE IS NO INTEROP, and hence you do not need those attributes.

I suppose they don't really hurt either (I am not sure if the compiler treats the struct any differently with the Interop attributed declared...it might)...but they certainly are not needed.

Does that clear things up? I guess bottom line, we can simplify your code, remove unecessary attributes, remove the custom structs (for now...just to get through the reading part) and concentrate on the problems with reading the data correctly.

PS. I may not get to respond again until monday. Apologies.
Nov 9 '08 #4
knuckels23
3 New Member
If the VB integers are two bytes, then you were correct to use short.

Looking over the two structs, it certainly looks correct to me, but of course one would have to test and verify. Not sure about 29 and 37 where you call the VB Double a C# int, but I assume you determined it was the correct match.

OK, I may be missing something, but if indeed you are simply reading a file, be it binary or ascii, using C#, you do not need the Interop attributes.

Interop means a .net (managed) application is talking directly to a non-.NET (unmanaged) application and exchanging data via structures, such as you created.

Although the VB app wrote the data to a file where each record represents the VB type you posted, ONCE IT IS WRITTEN the file has no concept of those structures. It is simply character or binary data.

Now, in order to read it, the .Net IO classes only see a file with a series of bytes. IF the content is text, the TextReaders need to know the encoding and what the EndOfLine character is, and they are happily off reading it.

If the content is binary, which is probably your case, then yes, the BinaryReader has read methods which need to know the data type it is reading. For example: ReadInt16() will read two bytes from the stream and output a short which you can than store or use however you want.

However, it does not need to know about String150[] for example, or any other types beyond primitive. If there are 20 strings each 150 characters, then you can either call ReadChars(150) 20 times, or even ReadChars(3000) . Once you have that data, then yes, you can store it in a struct like the one you created in order to emulate the VB source. But the binary reader only needs to know primitive types.

Finally, even if you do create your own struct and fill it using the BinaryReader, as long as you are not trying to send it back to the VB application, or get it from the VB application, THERE IS NO INTEROP, and hence you do not need those attributes.

I suppose they don't really hurt either (I am not sure if the compiler treats the struct any differently with the Interop attributed declared...it might)...but they certainly are not needed.

Does that clear things up? I guess bottom line, we can simplify your code, remove unecessary attributes, remove the custom structs (for now...just to get through the reading part) and concentrate on the problems with reading the data correctly.

PS. I may not get to respond again until monday. Apologies.
Hi,

Apologies for the delayed updation.
The issue was with the Double Data type.
I have managed to fix the problem.
Thanks for you help.
regards,
karthik
Nov 12 '08 #5

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

Similar topics

7
7017
by: Oin Zea | last post by:
Is it possible for to program to access a random file at the same time and perform actions like create a new record?
14
4592
by: Job Lot | last post by:
I have tab delimited text file which gets populated on daily basis via automated process. New entry is written at the bottom. I need to create a utility which makes a copy of this file with 10 most recent entries. When I read line using StreamReader object it starts from the top, so looping though lines and keeping track of the line is not helpful. Is there anyway to start reading from the bottom or if anyone could suggest some other...
8
2908
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way to load them all into memory. I've tried using vector pushback with reserving, but it was horribly slow. The current method I am using is upon opening the file and reading the number of values, resizing the vectors (I have 3, one for each data...
1
6769
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but listen up; Reports, the way I look at them, all present data downwards, in this way; TITLE data
15
2210
by: djj858 | last post by:
Another newbie question: How do I begin reading data, but starting from the xth line down a list? In other words, how do I skip the first lines and not read in those values?
4
1145
by: bnob | last post by:
I must read a txt file and when I found a line beginning with the word "Backup", I must replace this line with "Save" The read and the write must done into the same file! I know how to read line after line a file (I use StreamReader), but how can I write during I read the file ?? -- Ceci est une signature automatique de MesNews.
3
2363
by: VB.NET | last post by:
I'm using a mysql database and connecting my vb.net program to the DB over a network connection. i would like to bring this data over to a vb.net random access file. does anyone know how to convert my DB into a binary file? and how to convert my code to use a file instead of the db?
12
3059
by: Yi Xing | last post by:
Hi All, I want to read specific lines of a huge txt file (I know the line #). Each line might have different sizes. Is there a convenient and fast way of doing this in Python? Thanks. Yi Xing
0
1642
by: kaminekutte | last post by:
Hi everybody, I have been trying to parse a 100MB log file(tab separated). Basic aim is to read the file randomly, do some procesing and then display the contents of the file line by line. Working with randon access file class makes reading very slow. Is there any alternative to random access files class in java whereby faster file processing is possible??
0
9975
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9812
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11212
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10799
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10446
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8004
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7154
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4255
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.