TextReader.ReadLine()无法读取整行

jp2*_*ode 3 c#

我有一个逗号分隔文本文件,我正在尝试阅读.

我一次读一行,并处理该信息.

使用下面的代码片段和文件片段,当我到达以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();
}
Run Code Online (Sandbox Code Playgroud)

文件片段(从第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.
Run Code Online (Sandbox Code Playgroud)

",MTF#264653要添加衬套27256和28997 T恤,以便使用适合经销商的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.,,,
Run Code Online (Sandbox Code Playgroud)

看!^^^ SO的读者也做了!

以下是以841开头的行的确切文本:

841,,1/7/2011,588945,,171157,JC,V3B ,, Tom Read,D,"装配图AD-V3B-162C-EPSSTLDR有一个7/8分配器连接到T恤上的5/8开口.",MTF#264653要添加衬套27256和28997 T恤,以便使用适合经销商的T恤.,,,

仅供参考:我正在使用C#开发.NET Framework 4.

[解决]我能够使用Rob Parker的评论并使用原始Stream而不是更漂亮的TextReader类来解决这个问题.事实证明我的Rogue角色是一个插入的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);
}
Run Code Online (Sandbox Code Playgroud)

谢谢大家的帮助!

Nic*_*rey 5

TextReader 将以下任何字符视为行尾分隔符(它尝试与各种行尾惯例一起使用):

  • CR.
    旧的MacOS(OS X之前版本)终端约定:"\r".
  • CR + LF.
    Microsoft Windows/DOS行尾约定:"\r\n".
  • 如果.
    *nix行尾惯例:"\n".

我怀疑你\r的某个地方有一个假的(CR)漂浮着.