?
Solved

Reading only a section of one record

Posted on 2006-11-12
3
Medium Priority
?
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 35

Accepted Solution

by:
Raynard7 earned 1500 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month8 days, 2 hours left to enroll

765 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