2011-08-31 84 views
3

我已经得到了我想要在读书,我在1线一次读一个逗号分隔的文本文件。TextReader.ReadLine()无法读取整条生产线

,并处理这些信息。

使用下面的代码片段和文件片段,当我到达以841开头的行时,出现错误 - 它只能拉出147个字符。

问题:是什么原因导致TextReader停止拉线?它有一些特殊的顺序吗?

代码段:

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(); 
} 

文件片段(从第0行至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到添加衬套27256和28997三通为了使用一个适合进入分销商的T恤。,,,

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.的读者也是这么做的!

这是该行的确切文字与841开头:

841,1/7/2011,588945,171157,JC,V3B ,,汤姆读,d,“装配图AD-V3B-162C-EPSSTLDR有一个7/8分配器连接到三通的5/8开口, “,MTF#264653到添加衬套27256和28997三通,以便使用适合分配器的三通。,,,

FYI:我在C#开发针对.NET Framework 4的

[已解决]我能够使用Rob Parker的评论和使用原始Stream而不是更漂亮的TextReader类来解决这个问题。事实证明,我的流氓角色是插入的回车(\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); 
} 

感谢大家的帮助!

+0

什么是您的编码设置? –

+0

我可以将它设置为任何内容。我坐在记事本中。默认情况下,它显示ANSI。 – jp2code

+3

可能是嵌入换行符或换行符,很难说没有实际的字节。在此页面上查看源文件显示您的确切文本实际上有两行,在'tee.'后面有一个新行。 – user7116

回答

1

因为它原来是特别有帮助......

你检查什么字符(县)有在那里它分割线的点的周期和双引号字符之间?

如果ReadLine()在返回的内容中不包含换行符,您可能需要做一些工作才能找到它/它们。但是如果你可以得到TextReader使用的FileStream对象(不知道它是否被暴露),你可以添加代码来检测问题行(从“841”开始),然后敲一个断点(或Debugger.Break()),然后使用底层的FileStream来备份位置并读取原始字节以查看有哪些内容。

+0

我最终做的事情是使用'TextReader'在整个文件中读取,然后使用'TextWriter'将其逐行写回* File2.csv *。接下来,我在记事本中打开了* File2.csv *,并手动修复了所有的换行符错误。感谢您指出了这一点! – jp2code

+1

因此,我假设您没有将它看作是原始文件中的换行符,因为它只是\ n不是Windows使用的\ r \ n。通过使用TextWriter重写文件,您可以将伪造的换行符转换为完整的Windows \ r \ n换行符,然后您可以清楚地看到该文件格式的错误并在编辑器中手动更正它们。 –

+0

这是100%正确的。 – jp2code

5

TextReader治疗以下任何字符作为结束线的定界符(它试图发挥好与最终的线的各种约定在那里):

  • CR。
    旧的MacOS(pre-OS X)行尾约定:"\r"
  • CR + LF。
    Microsoft Windows/DOS行尾约定:"\r\n"
  • LF。
    * nix行尾约定:"\n"

我的怀疑是你有一个伪造的\r(CR)在他们的某处浮动。

+0

Rob Parker首先发现了这一点。如果他写了,我会改变这个并给他答案,否则它是你的。不知何故,其中一位工程师在文本字段中复制了一个'\ n'。 – jp2code