gpt4 book ai didi

c# - TextReader.ReadLine() 无法读取整行

转载 作者:行者123 更新时间:2023-11-30 14:38:51 26 4
gpt4 key购买 nike

我有一个逗号分隔的文本文件,我正在尝试读入。

我一次读 1 行,然后处理该信息。

使用下面的代码片段和文件片段,当我到达以 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 三通,以便使用适合分配器的三通。,,,

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.,,,

看! ^^^ S.O. 的读者也做到了!

这是以 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.,,,

仅供引用:我正在针对 .NET Framework 4 使用 C# 进行开发。

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

感谢大家的帮助!

最佳答案

TextReader 将以下任何字符视为行尾定界符(它试图很好地适应各种行尾约定):

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

我怀疑你有一个虚假的 \r (CR) 漂浮在他们的某个地方。

关于c# - TextReader.ReadLine() 无法读取整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7260959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com