开发者

Parsing sequential ASCII file for editing - Visual C#

I have been recently assigned a task in which I need to create a program to edit files in Visual C#. The file is a sequential ASCII text file with multiple fixed character width rows, and the basic structure looks like this:

Each group is a single parameter, and there can be hundreds of these within a file. Each PA has a set number of columns, but each PA varies in column numbers. Additionally, there are some optional fields within each record, so that accounts for the extra spaces between some of the fields. Each field also has a separator of 1 space. I am unsure if I am being too thorough, but I figure the more information I give, the easier it will be for someone to help me.

Essentially, I want to create a GUI in which the user can select one a parameter mnemonic and then be able to edit any of the fields within the entire group of parameters

I have a little C# experience, but I am having trouble figuring out what would be the best way to accomplish this. My initial thinking was to parse the file into an array, search for each parameter name (maybe using regular expressions?), display all the parameter names in a browser. From there the user would be able to select which parameter they wish to edit. The program would then search the array again for the selected parameter, and then parse the records into text boxes, to allow for editing.

I am unsure if this is the most efficient way of accomplishing this, and I am somewhat stuck on how to actually parse the file to read the field开发者_运维技巧s into the text boxes. I have searched many different forums for file parsing, but most of the topics I find related to comma delimited files which does not really apply to the file type I am working with. Any help would be greatly appreciated, and if you need me to elaborate on anything, please feel free to ask me. Thank for the help.


This isn't C# advice, but more general programming advice. If I were you, I would create a class to represent each PA you have, and have them each inherit from the interface IPAObject (or something like that). That way you can treat them as subtypes of a abstract PA object.

Then, I would make an class called PAGroup, which holds an array of IPAObject's.

Each individual concrete PA object class would then have fields for each parameter it has, and can be responsible for serializing itself to or deserializing itself from a stream (in your case, this stream is probably an input or output file), using C# file API's. You could use regular expressions, but since each PA object has fixed-length lines, you might as well read the line in all at once, using ReadLine() and parse it using array operations, like you said yourself. If you go this route, my advice to you is to store offsets for each columns starting offset and length in each class object, rather than littering your code with a whole bunch of literal numbers.

These classes would also each contain the logic for what their columns are and how to modify that data. Some of your columns seem open ended, so they would probably just be represented by strings. Others look like they come from a finite list of choices. These would probably be represented by constants, which belong to the relevant PA object class. The class would be responsible for converting the constants and other object to and from the actual ASCII representation from the file.

This way, when you read the whole PA file, you end up with a nice hierarchical data structure that represents the structure of your file, and from a coding perspective, all of the logic is separated very nicely, which makes it very easy to maintain. This nice data structure could serve as the model in your model-view-controller architecture. Then you just need to worry about implementing the user interface for interacting with your data.

I hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜