Hi,
I have a record that has a fixed length of 253 characters. The record is split into multiple sub- records each of a different length.
The first sub-record has a minimum length of 125 and I need to process only this.
Doing something like this :
if(record. length > 124){
//process record
}
will work for records that have a minimum first sub-record length of 129 as subsequent sub-records will be <= 124 (253-129 = 124) and will be skipped.
But in a case, where the the record is split into 126 and 127, the above code will not work as it will read both the sub-records ( and I need only the one that is 126).
Can somebody tell me how I can solve this problem and read only the first sub-record .
Thanks much!!
Praveen

Reading the first half of a split record with C#
kkorolz
very confusing. Why dont you check the record length to see if it is 126 Would this not work Where are you obtaining the records from Why are they being "split" *drinks more coffee*
what about say extracting the first 126 characters Would this not work ....
string theFirstRecord = theRecord.SubString(0, 126);
Sideout
ok...why dont you make the records in the textfile a comma seperated type of record so for each column, there will be a comma. From this we can read and split records by the comma and get the field/column we want.
however I guess you could split it by the spaces but this would result in a small problem in that if a column has spaces, it will also split that, which is not what we want. it is better to do it by a comma. Either way it goes something like this:
using (StreamReader theReader = new StreamReader("file.txt"))
{
while (!theReader.EndOfFile)
{
string[] theCurrentRecord = theReader.ReadLine.Split(new char[]{','});
}
}
you would replace comma with a space (empty space).
this will go through each line of the text file, read the line and split it by the specified delimeter into a string array.
hazz
Thanks
SomeGuyOnAComputer
Will this do
if (record.Length == 253)
{
//OK process whole record
string field1 = record.Substring(0, 20);
string field2 = record.Substring(21, 40);
}
else if (record.Length > 124)
{
//Record broken into subrecords
// 'record' now holds the 125 byte part
//Process record
string field1 = record.SubString(0, 20);
....
//Swallow the second part of this broken record and
//move the read-position past it.
read.ReadLine();
//The 2 remaining linebreaks will be swallowed during
//the next few loops of the while-loop since these
//are < 124
}
Edit: A second approach might be to use a boolean indicator to let you know if the split record should be processed or not. This leaves the ReadLine() call to be executed at one place only.
bool splitRecordProcessed = false;
while (record = read.ReadLine())
{
if (record.Length == 253)
{
//Process record
string field1 = record.SubString(0, 20);
...
}
else if (record.Length > 124)
{
if (!splitRecordProcessed)
{
// Process record
string field1 = record.SubString(0, 20);
...
splitRecordProcessed = true;
}
else
{
splitRecordProcessed = false;
}
}
}
You should consider refactor the 'process record' part in a separate method since its identical for both records..
Regards,
wwfDev
crazy_boss
wow. Making life hard for yourself :-) Should have used SQL Server or MS Access
Sorry for being slow here but are you trying to read each "field" from the record you could still do a split, if this is the case, on the empty space and still do the above method mentioned but instead using the space as a delimeter....problem being if the record has a field which contains spaces (like firstname lastname) then it will split on that too, which is not what you want.
Shawn Rheal
Actually,no. Per my previous post, I am not concerned about the record with a length of 253 (runs from 0 to 252), it is only when it is not 253 that I am concerned about, when the record is split into two sub-records of lengths 125 and 128 and per my sample code I will be reading both when I want to read only the one that is 125.
I think I will re-visit this at a later time. Thanks for your help
Bartosz
it makes sense but hard to explain I guess in a sense.
I guess you can read each record line by line, check the length of the line. if it is of a specified length, then we are in business so split the current line into a string[] array using the space as a delimeter, then do what we want in that string[] array (at this point it will not write back changes to the file if you are after this). Example for this:
using (StreamReader theReader = new StreamReader("file.txt"))
{
while(theReader.EndOfFile == false)
{
string theLine = theReader.ReadLine();
if (theLine.Length == 254)
{
string[] theCurrentRecord = theLine.Split(new char[] {' '});
//do operation on "theCurrentRecord"
}
}
}
so this will keep reading each line, if current line being read is of total length of 254 (253 + 1, since length always adds 1 to the value we expect I believe), then it will split the current line we read into a string[] array by the space delimeter and we do our operation on it.
something that may fit your criteria
Sergio Ortiz
Didn't mean to confuse. The record is being read from a text file. Each record is being broken up into sub records. So, there can be a 253 character record with sub-records one going from 0 to 124 (125)and the other going from 0 to 125(126) and an additional two new line chars making a total of 253. The record breaks and falls in the next line as a sub-record. Each of these sub-records will be treated as different though they are part of the same record.
so if I do something like this
string data = record.Substring(0,126)
will load both the sub-records and I want only the first
Here is an example of such a record:
000259789 CHE 0411100980 7529017 7529352 R00010001A ( Length 125)
POSITIVE 3(Length 126)
I want only the 125 one.
Hope that makes sense.
Thanks
Nkenta
It's a huge text file with lots of such records. Are you suggesting that I should programmatically append the ',' to the first half of the record during my first run of the while loop and then during the second run I check to see which record has the ',' and then read only that half of the record Basically, using a tagging mechanism
Wouldn't your code add the ',' to the second sub-record also (during the first run of the while) in which case I would end up reading the second half as well during the second run . I hope I am not sounding too confusing.........
Thanks
adam99
Yes, I am parsing out each field from the record and doing a validation on the data in the field with some predefined rules. I am already doing the split by using the Substring method, my question is how can I make my program access only the fields that I need in the first sub-record and completely skip the next sub-records and move to the next 253 byte record.
Hope that makes sense.
Thanks!
Dr.Virusi
The records are not in the field1, field2, field 3, field 4 format. Each field in the record is of fixed length and all these lengths add up to 253.
So, what I am currently doing is :
while(record == read.ReadLine()){
if(record.Length == 253) //No problem the record is one single big record with fixed length
{
string field1 = record.Substring(0,20);
string field 2 = record.Substring(21,40);
.......................................
............................................
}
else if(record.Length != 253)
{ // Record is broken up into different sub records
if(record.Length > 124) { //Here is my problem, since this is going to read two sub-records each of length > 124 but < 253 when
//I want it to read only one as mentioned in my previous postings.
string field 1 = record.Substring(0,20);
string field 2 = record.Substring(21,40);
..............................................
........................................
}//End of if
}//End of else if
}//End of while
How should I modify this code, so I can read only the first part of the record
ManjuVijay
:-)
say for example we have a record in this format:
field1, field2, field3, field4
and we did use the code as above. The values returned would be like this in the string array:
field1
field2
field3
field4
and we can then just go through the array and choose which item we want to work with and do whatever we want with it. This would be so much easier than the current method perhaps.
It won't append anything to the file at all or change it in any way as it is just reading the file. Does this make sense