I've got a Comma Delimited Text file that I am trying to read in.
I read in 1 line at a time, and process that information.
Using the code snippet and file fragment below, my error comes when I get to the line that starts with 841
- it only pulls 开发者_如何学Goin 147 characters.
Question: What is causing the TextReader
to stop pulling in this line? Is there some special sequence in it?
Code Snippet:
int lastNum = -1;
int num = 1;
using (TextReader reader = File.OpenText(filename)) {
do {
string line = reader.ReadLine();
if (!String.IsNullOrEmpty(line)) {
string[] split = line.Split(',');
int indexer = Convert.ToInt32(split[0]);
Console.WriteLine("#{0}: ID '{1}' Line Length = {2}", num++, split[0], line.Length);
}
} while ((-1 < reader.Peek());
reader.Close();
}
File Fragment (from line 0 to ProblemLine
+ 1):
ID,Line,[Date],WO,Module,DSO,Integer,Unit,,Contact,Category,Problem,Solution,Action,Actor,Acted
824,,1/4/2011,589259,,170966,JC,V3A,,Tom Read,WO.3,"The unit is stainless steel, but the coil connection plates that were on the work order were not stainless steel",MTF # 264698 to take off CC500 AND CC875 and added XCC500 AND XCC875,,,
825,,1/4/2011,588779,,171102,JC,V3A,,,W.4,Changing from a 310AJ motor to a 310AX,MTF # 46746 to fan assembly and motor,,,
826,,1/4/2011,588948,,170941,JC,V3B,,,W.4,Changing from a 310AJ motor to a 310AX,MTF # 241092 and 241093 to change fan assemly and EBM motor,,,
827,,1/4/2011,588206,,171143,JC,H3A,,,WO.2,Potentiometer was missing from the work order,MTF # 264851 to add 29278,,,
828,,1/4/2011,584741 584742 584748 584747 584749,,171009,BF,V2B,,"Carlos, Laura",,Johnson units. Motors would not fit correctly using the motor mounts already installed.,MTF# S264510 to remove 006-300 motor mounts from work orders. MTF# S264699 to add 006-033 motor mounts to work orders.,,,
829,,1/4/2011,586519,,170891-1-2,DB,H3B,,"Carlos, Laura",WO.2,"1"" bushing not on BOM.",MTF# 264769 added 28614,,,
830,,1/4/2011,583814,,170804-1-3,DB,V3B,,"Carlos, Laura",WO.3,Wrong pulley (26710) and wrong Belt A-41 (29725) appear on WO.,MTF# 264570 removed those and put on an A-33 (26768) and pulley 27005. Two units so Qty 2 for each item.,,,
831,,1/5/2011,584742,,171009,JC,V2B,,,,there was an extra overload relay on the work order because it had been changed and the original was never taken off.,MTF # 241926 to take off 7- 27167 overload relay,,,
832,,1/5/2011,591742,,170965,JC,H3C,,"Carlos, Laura",WO.3,Belt was too short,MTF # 241729 to take off 30737 (BX42) and put on 28589 (BX52). Center to center distance was 19 3/8 in,,,
833,,1/5/2011,584749,,171009,JC,H2A,,Joe ,E.3,Did a motor change in order for the motor to work on the unit,MTF # 264854 to add 28918 and take off 28095 motor and SP01204 pulley,,,
834,,1/5/2011,588945,,171157,JC,V3B,,Alex,D,Stainless steel unit needed a stainless steel power entering cover plate.,Spoke with Alex and he designed X302-905 and MTF # 241094 was done to add to this work order.,,,
835,,1/5/2011,589259,,170966,JC,V3A,,Alex,D,Stainless steel unit needed a stainless steel power entering cover plate.,Spoke with Alex and he designed X302-905 and MTF # 241094 was done to add to this work order.,,,
836,,1/5/2011,584749,,171009,JC,H2A,,,,Changed overload relay because changed motor,MTF # 264857 to change overload relay. Took off 27169 and added 26736,,,
837,,1/6/2011,583815,,170804,JC,V3B,,"Carlos, Laura",WO.3,bore hole on the pulley was too big ,MTF # 241096 to take off 26710 7/8 pull and put on 27005 5/8 pulley,,,
838,,1/6/2011,583816,,170804,JC,V3B,,"Carlos, Laura",WO.3,bore hole on the pulley was too big ,MTF # 241096 to take off 26710 7/8 pull and put on 27005 5/8 pulley,,,
839,,1/6/2011,587632,,171143,BF,M2,,"Carlos, Laura",WO.2,H302-850 blank off #3 not on WO.,MTF# S242648 to add (1) H302-850,,,
840,,1/6/2011,583816,,170804,BF,M2,,"Carlos, Laura",WO.3,A41 Belt too large,"MTF# S241706 to remove A41 (29725) and add A33 (26780). C-C distance 12.5",,,
841,,1/7/2011,588945,,171157,JC,V3B,,Tom Read ,D,"Assembly drawing AD-V3B-162C-EPSSTLDR had a 7/8 distributor connecting to a 5/8 opening on a tee.
",MTF # 264653 to to add bushing 27256 and 28997 tee in order to use a tee that would fit into the distributor.,,,
842,,1/7/2011,589257,,170966,JC,V3C,,Everyone ,WO.2,heat exchanger was missing from the work order ,MTF # 264858 to add the heat exchanger on work order and one was ordered.,,,
LOOK! ^^^ S.O.'s reader did it too!
Here is the exact text of the line that starts with 841:
841,,1/7/2011,588945,,171157,JC,V3B,,Tom Read ,D,"Assembly drawing AD-V3B-162C-EPSSTLDR had a 7/8 distributor connecting to a 5/8 opening on a tee. ",MTF # 264653 to to add bushing 27256 and 28997 tee in order to use a tee that would fit into the distributor.,,,
FYI: I am developing in C# against .NET Framework 4.
[Solved] I was able to figure this out using Rob Parker's comment and using a raw Stream
instead of the prettier TextReader
class. It turns out my Rogue character was an inserted Carriage Return (\n
).
using (Stream fs = File.Open(filename, FileMode.Open, FileAccess.Read)) {
byte[] data = new byte[1024];
int len;
do {
len = fs.Read(data, 0, data.Length);
for (int n = 0; n < len; n++) {
if ((n + 3) < len) {
string strId = string.Format("{0}{1}{2}", (char)data[n + 1], (char)data[n + 2], (char)data[n + 3]);
int numeric = Convert.ToInt32(strId);
if (numeric == 841) {
char[] suspects = new char[50];
int n2 = n;
int n3 = 0;
while (n2 < len) {
if ((n + 130 < n2) && (n2 < n + 160)) {
suspects[n3++] = (char)data[n2];
}
n2++;
}
Console.WriteLine("Wait Here!");
break;
}
}
}
num++;
} while (0 < len);
}
Thanks everyone for your help!
TextReader
treats any of the following characters as an end-of-line delimiter (it tries to play nice with the various end-of-line conventions out there):
- CR.
The old MacOS (pre-OS X) end-of-line convention:"\r"
. - CR+LF.
The Microsoft Windows/DOS end-of-line convention:"\r\n"
. - LF.
The *nix end-of-line convention:"\n"
.
My suspicion is that you've got a spurious \r
(CR) floating around in their somewhere.
Since it turned out to be particularly helpful...
Have you checked what character(s) there are between the period and doublequote character at the point where it's splitting the line?
If ReadLine() doesn't include the line-break characters in what it returns you might have to do a little work to get to it/them. But if you can get the FileStream object used by the TextReader (not sure if it's exposed) you could add code to detect the problem line (starting "841,") and hit a breakpoint (or Debugger.Break()) and then use the underlying FileStream to back up the Position and read the raw bytes to see what's there.
精彩评论