Solved

Reading only a section of one record

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expression Evaluater 3 46
locate sql commands in C# visual studio Project 6 44
C# Windows Form Navigation - Total Beginner 9 48
Build a string of emails from a gridview 2 18
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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