当前位置:编程学习 > C#/ASP.NET >>

ReadLine()方法读取文本文件中的一行记录将其赋给String变量出错

            StreamReader reader = new StreamReader(fileStream, Encoding.Default);
            string readString = null;
            StreamWriter writer = new StreamWriter(fileStream);
            string writeString = null;

            string[] DataS = null;
            //string delimStr = ",";
            //char[] delimiter = delimStr.ToCharArray();
            string strTemp;
            string strIoSort;

            long lBarcodeFilePos = -127;
            while ((readString = reader.ReadLine()) != null)
            {
                if (readString.Length == 0)
                    break;

                lBarcodeFilePos += 127;

                DataS = readString.Split(new char[] { ','});
                // 此条记录是否已经被删除,0,未删除。1,已删除。
                strTemp = DataS[0];
                if (strTemp == "1")
                    continue ;

                strTemp = DataS[5];
                if (strTemp.Trim() == BillID.Trim())
                {
                    strIoSort = DataS[1];
                    strTemp =DataS[1].Trim() ;
                    if (strTemp == IoSort)
                    {
                        writeString = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}{17}{18}{19}",
                                "1,",
                                strIoSort,
                                ",",
                                DataS[2],
                                ",",
                                DataS[3],
                                ",",
                                DataS[4],
                                ",",
                                DataS[5],
                                ",",
                                DataS[6],
                                ",",
                                DataS[7],
                                ",",
                                DataS[8],
                                ",",
                                DataS[9],
                                ",",
                                DataS[10]
                                );
                        fileStream.Seek(lBarcodeFilePos, SeekOrigin.Begin);
                        writer.WriteLine(writeString);
                        writer.Flush();                        
                    }
                }                
            }
            reader.Close();
            //writer.Close();
            fileStream.Close();

在上面程序中,我读取一个文本文件中的行记录。每次读到N*8+1(N,1,2,3,4,5,……N) 行的记录,并将读取的值赋给:readString。总出错。读取的值正确,但赋值时,多出来两个字段(我行记录中的字段个数:11),且这两个字段的值,还有特点。请各位大侠给指点迷津。跪谢。

--------------------编程问答-------------------- 缺省情况下 FileStream 是独占的,你这样同时读写的做法是不规范的。而且你应该给出详细错误信息,而不是“出错”两个字就够了。 --------------------编程问答--------------------
引用 1 楼 secretgarden 的回复:
缺省情况下 FileStream 是独占的,你这样同时读写的做法是不规范的。而且你应该给出详细错误信息,而不是“出错”两个字就够了。


没错。另外应该把你的测试输入文件也贴出来
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,