C#streamreader读取txt错误 [英] C# streamreader reading txt error

查看:63
本文介绍了C#streamreader读取txt错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int  i =  0 ; 
StreamReader reader = new StreamReader( @ 3235.txt,System.Text.Encoding.Default);
string line = reader.ReadLine();
var row = new DataGridViewRow();
dataGridView1.Rows.Add();
dataGridView1.Rows [i] .Cells [ 0 ]。值= line.Substring( 40 6 );







我的文字:

0409253100GRİTALYANSUCUK137 925310 0VAN-PA GIDA PAZ.TUR。 028273 
040925380GR.İTALYANSUCUK137 925310 0VAN-PA GIDA PAZ.TUR。 028273



i无法理解为什么这段代码会读取第二行txt文件?

当我运行代码时我只得到了040925380GR.İTALYANSUCUK137 925310 0VAN-PA GIDA PAZ.TUR。 028273



我尝试过:



在txt中添加空行文件它工作正常,这不是我想要的。

解决方案

你可以读取每一行并添加到datagrid这样。



  int  i =  0 ; 

使用(StreamReader reader = new StreamReader( @ 3235.txt,System.Text.Encoding.Default))
{
while (reader.Peek()> = 0
{
string line = reader.ReadLine();
var row = new DataGridViewRow();
dataGridView1.Rows.Add();
dataGridView1.Rows [i] .Cells [ 0 ]。值= line.Substring( 40 6 );
i ++;
}
}


如果您的文本文件正是您所显示的内容:

第1行
第2行

然后你会得到一个ArgumentOutOfRangeException而不是第二行。

所以我怀疑你的文本文件并不完全符合您的想法。

使用调试器。在该行上设置一个断点:

  var  row =  DataGridViewRow(); 

然后使用它来查看中的确切内容 - 我的猜测是它包含第1行后跟34个空格和第2行而不是你的想法...


你所展示的代码看起来不可能从第二行开始读取文件。

使用调试器查看中的内容,首先是 reader.ReadLine()



调试器允许你逐行跟踪执行,检查变量,你会发现它有一个点停止你做的事情。

< a href =https://en.wikipedia.org/wiki/Debugger>调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

当代码没有按预期执行时,您接近于错误。

int i = 0;
StreamReader reader = new StreamReader(@"3235.txt", System.Text.Encoding.Default);
    string line = reader.ReadLine();
var row = new DataGridViewRow();
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = line.Substring(40, 6) ;




my text:

0409253100GRİTALYAN SUCUK         137     925310  0VAN-PA GIDA PAZ.TUR.               028273
040925380GR.İTALYAN SUCUK         137     925310  0VAN-PA GIDA PAZ.TUR.               028273


i cant understand why this code read second line of txt files?
when i run the code i got only 040925380GR.İTALYAN SUCUK 137 925310 0VAN-PA GIDA PAZ.TUR. 028273

What I have tried:

adding empty line in txt file it works fine how ever thats not my want.

解决方案

You can read each lines and add to datagrid like this.

int i = 0;

               using (StreamReader reader = new StreamReader(@"3235.txt", System.Text.Encoding.Default))
               {
                   while (reader.Peek() >= 0)
                   {
                       string line = reader.ReadLine();
                       var row = new DataGridViewRow();
                       dataGridView1.Rows.Add();
                       dataGridView1.Rows[i].Cells[0].Value = line.Substring(40, 6);
                       i++;
                   }
               }


If your text file is exactly what you show:

Line 1
Line 2

Then what you will get is an ArgumentOutOfRangeException rather than "the second line".
So I suspect that your text file doesn't hold exactly what you think it does.
Use the debugger. Put a breakpoint on the line:

var row = new DataGridViewRow();

And use it to look at exactly what ins in line - my guess is that it contains "Line 1" followed by 34 spaces and the "Line 2" rather than what you think...


It look impossible that the code you show start reading the file by the second line.
Use the debugger to see what is put in line by first reader.ReadLine().

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


这篇关于C#streamreader读取txt错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆