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
861 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 54

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 54

Accepted Solution

by:
Julian Hansen earned 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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