Solved

Reading only a section of one record

Posted on 2006-11-12
3
162 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
Comment Utility
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
Comment Utility
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
Comment Utility
 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now