Click here to Skip to main content
15,907,396 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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.
Posted
Updated 21-Mar-19 10:35am
v4

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

C#
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++;
                   }
               }
 
Share this answer
 
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:
C#
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...
 
Share this answer
 
Comments
Member 10525430 3-Aug-16 6:45am    
sorry i write it examply

i add original txt lines
OriginalGriff 3-Aug-16 6:47am    
Use the debugger and look at what you get in "line" - we can't do that for you!
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.
 
Share this answer
 
I have same error reading by StreamReader. Don't know why it happens. In other Text/Hex Editor this lines looks good.

Possible one of these things gives an error in my case:
- File too big ~200GB.
- File with NTFS-compression. Disk error?
- File have newlines in unix-format "\n" not "\r\n".
 
Share this answer
 
Comments
geograph 21-Mar-19 16:52pm    
Ok, my solution read by one byte from "\n" to "\n"

int chr;
var bytes = new List<byte>();
while ((chr = fileInput.ReadByte()) != -1)
{
if (chr == '\n')
{
var line = Encoding.UTF8.GetString(bytes.ToArray());
fileOutput.WriteLine(line);
bytes.Clear();
continue;
}
bytes.Add((byte)chr);
}
CHill60 21-Mar-19 18:06pm    
If you have a question then use the red "Ask a Question" link at the top of this page. Do not post comments or questions as solutions to other members questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900