parsing text files with multiple formats with vc#2005

Source: Internet
Author: User

Generally speaking, a text file will only contain one format (for example, comma-delimited or fixed-field width), but the text file is likely to contain multiple formats, what should you do when you encounter this situation?

If your text file contains multiple formats, you should use the PeekChars method of the TextFieldParser object to obtain a specific number of characters from scratch to determine the format of the data row. Then tell the TextFieldParser object The format of the data row and read the data row. The PeekChars method returns only the specified number of characters and does not advance to the next line, by which the format of each data row is identified and the rows are read row by row, enough to parse text files that contain multiple formats and read them smoothly.

For example, suppose that we have a text file named "Rich Text file. txt" in the text folder of the application project, which is special in that it contains the following three formats:

• Data rows with fixed field widths of 5, 10, and-1 respectively.

• Data rows with fixed field widths of 6, 10, 17, and 1, respectively.

• Comma-delimited rows of data.

One feature of the data rows in these three formats is that the first characters of the data rows are CK, Pb, and SP, so we simply get the top two characters of the data row by the TextFieldParser object's PeekChars method, and then according to its value To set the TextFieldType property, and set the Delimiters property (or call the Setdelimiters method) or the FieldWidths property (or invoke the SetFieldWidths method), and use the ReadFields side method to read rows of data. Repeated use of this method to process each row of data, you can successfully parse and read the entire text file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.