?
Solved

How to write output records to a fixed length file using C# code in a console application with VS2010?

Posted on 2013-06-20
4
Medium Priority
?
944 Views
Last Modified: 2013-06-23
I am working with VS2010 and C#. I have attached an input file that has no file exension.
This file has fixed length records of 252 bytest that start with the sequence
"51ELRC" in the first 6 positons. These records can exist starting in any location within the input file. How would your write C# code as a console application to extract all 252 byte records that start with the character sequence "51ELRC" and write them to the output
file titled output.txt with a fixed record length of 252 bytes?
sample
0
Comment
Question by:zimmer9
  • 2
  • 2
4 Comments
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 39267840
Your input records are not all 252 bytes - the records that don't start with 51ELRC are 252 bytes - there are 6 of them but then you get teh first target record which is 282 bytes followed by another that is 271 bytes and another that is 318 bytes.

There does not appear to be anything in the record that indicates a size.
0
 

Author Comment

by:zimmer9
ID: 39268444
I am attaching a replacement input file. Sorry for the record layout problem with my original input file. In the new file:

I opened this file in TEXTPAD and observed the following:

the first record for type 51ELRC is in positions 1513 through 1764
the second record for type 51ELRC is in positions 19 through 270
the third record for type 51ELRC is in positions 13 through 264
the fourth record for type 51ELRC is in positions 41 through 292
Copy-of-sample
0
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 39269273
I am not seeing that and your numbers don't make sense.

You have records overlapping.

In the attached file I have the first record at Hex: 0x5E8
The second record starts at 0x703
Size: 283 bytes

The third record starts 0x81E
Size: 283bytes

The fourth record starts at 0x94D
Size: 303 Bytes

End of file is at 0xA4B
Size: 254 Bytes

Each of the target records seems to be terminated by a J* followed by a variable number of CR/LF and spaces.

If you remove the CR/LF and spaces then the file shows the records as fixed length - this is where the challange is - how to read the lines as binary but ignoring the white space.

One solution is to read the file into memory (without knowing more about your data and how big these files will get - this may not be the best solution). Assuming you can do this - you then process the input by looping through the data 1 byte at a time looking for the key sequence. You then copy out the 252 bytes you need and write to the file - jump to the end of the record and continue scanning bytes.
0
 

Author Comment

by:zimmer9
ID: 39269788
Okay then, since you are not seeing the record layout as I describe it, we can just focus on the record layout as follows, for the records that have the value "51ELRC" in starting positions 1 through 6 and records of these type have a fixed size length of 252 byes. Text fields are in ASCII format. The records with the value "51ELRC" are present throughtout the file in various starting positions.

column         data type         positions
---------         -------------        ------------
Field 1          Text                 1 - 46
Field 2          Binary               47 - 118
Field 3          Text                 119
Field 4          Binary               120 - 132
Field 5          Text                 133 - 149
Field 6          Binary               150 - 162
Field 7          Text                  163 - 170
Field 8          Binary               171 - 183
Field 9          Text                  184 - 203
Field 10        Binary                204 - 216
Field 11        Text                  217 - 228
Field 12        Binary               229 - 241
Field 13        Text                 242 - 252
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question