Solved

Reading only a section of one record

Posted on 2006-11-12
3
163 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 74
C# HTTP GET method sample code 3 55
how to read json value 2 48
Facial recognition to look through the whole database for a person 3 61
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

816 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

8 Experts available now in Live!

Get 1:1 Help Now