Solved

Reading only a section of one record

Posted on 2006-11-12
3
164 Views
Last Modified: 2010-04-16
Hi,

I am trying to read and parse a record of fixed length 253 bytes from a .txt file. Of the 253 bytes, I need to process only the first 124 bytes. The problem is there is a new line character in the record because of which a section of the  record ( starting from byte 125) falls into the next line as shown below.

The number of bytes resets to 0 and the program treats the bottom section as a new record though it is still one record.

000580667           TIHH             0400600020                            7529100                       7529352   R00010001A
POSITIVE                                                                                                                     3

My question is:

How can i read only the top section(where I have the first 124 bytes) of the record and completely skip the bottom section?

Doing something like this in my while loop:

while((record == read.ReadLine()) ! = null){
string data = record.Substring(0,124);
}
would fetch both the top and bottom sections and I need to process only the top section.

Can somebody help me with this problem?

Thanks
0
Comment
Question by:pdurbha
3 Comments
 
LVL 35

Accepted Solution

by:
Raynard7 earned 500 total points
ID: 17927332
Just put a bool in so you can alternate them

bool thisLine = true;
while((record == read.ReadLine()) ! = null){
    if(thisLine) {
        string data = record.Substring(0,124);
        thisLine = false;
    } else {
        thisLine = true;
    }
}
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17928796
You could also not read lines but read bytes.
If each record is fixed with you could read 124 bytes and then skip 129 bytes, then read 124 bytes etc.
0
 
LVL 7

Expert Comment

by:mjmarlow
ID: 17929006
 You could also read it directly using BinaryReader.  

        using System.IO;
        /// <summary>
        ///  (untested) Example of reading a single record from a fixed
        /// length record datafile.  
        /// </summary>
        /// <param name="filename">path and name of fixed length file</param>
        /// <param name="recordNumber">0 = first record</param>
        /// <param name="recordSize">Length</param>
        public string  GetData(string filename, int recordNumber, int recordSize)
        {
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader r = new BinaryReader(fs);
            r.BaseStream.Seek(recordNumber * recordSize);
            char[] buffer = r.ReadChars(recordsize);
            string data = new string(buffer, 0, buffer.Length);
            r.Close();
            fs.Close();
            return data;
        }
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!

807 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