C#怎么读取txt

感冒了多喝热水 2016-11-22 03:55:19
格式,如:
111 222 333
222 333 444
333 444 555
...
求解!!要是碰到格式不对应该怎样弄..
...全文
177 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
EnForGrass 2016-11-23
  • 打赏
  • 举报
回复
谁知道什么格式才正确,。 一行一行地读取,再对每一行处理
  • 打赏
  • 举报
回复
        static void Main(string[] args)
        {
            int lineCount = 0;   //行数
            string lineStr;         //每行字符串内容
            StreamReader reader = new StreamReader(@"test.txt");
            while ((lineStr=reader.ReadLine())!=null)
            {
                string[] temp= lineStr.Split(' '); //按空格分隔
                for (int i = 0; i < temp.Length; i++)
                {
                    Console.WriteLine(temp[i]);  //逐一输出
                }
                lineCount++;   
            }
            reader.Close();
            Console.WriteLine("Total lines:" + lineCount);
            Console.ReadLine();
        }
  • 打赏
  • 举报
回复
File.ReadLine 一行行读,然后正则判断下当前行是否符合规则,格式不对就是你业务逻辑问题了,别人怎么可能知道

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧