简体   繁体   中英

Can't read a file line-by-line in C#

I've been trying to read a file line by line for my UNI project. I am getting an error that I am not sure I understand. I would need your experiene to help me solve it out, please.

Some clarification for the code: datas is a List, which has a custom class type which class has 3 properties: text1, text2, int1. v is a simple object with the same custom class type as the datas List. The data in the text file are in line-by-line, each line contains 1 value for the 3 properties like this: text1value;text2value;int1value.

if (File.Exists("example.txt"))
        {
            StreamReader sr = new StreamReader("example.txt");
            while(!sr.EndOfStream)
            {
                string[] data = sr.ReadLine().Split(';');
                v.text1 = data[0];
                v.text2 = data[1];
                v.int1 = Convert.ToInt32(data[2]);
                datas.Add(v);
            }
            sr.Close();

Thanks to you guys I have made improvements on my code and made it work! Now I only have 1 functionality error which I do not understand on the code which is after the read in is completed. (so the code runs without error, crash, etc. - but gives the wrong result SOMETIMES!).

            int i = 0;
            int cnt = datas.Count;
            while (i < cnt)
            {
                if (datas[i].Text1 == tb_Text1.Text && datas[i].Text2 == tb_Text2.Text)
                {
                    // I do stuff here with the correct combination
                    DialogResult = DialogResult.OK;
                    break;
                }
                else
                {
                    i++;
                }
            }
             if(i==cnt)
                {
                    MessageBox.Show("The following combination is not in the txt file!");
                }
         }

So in the second part of the code, on the Windows Form, there are 2 textboxes: one is for the text1 property, the other is for the text2 property.

I would like it to work like it would in a username-password scenario. If the user types a text1 and text2 value in the textboxes, and clicks on the button which is on the Form, and that specific text1 and text2 values are stored in the same line of the txt file which was read in in the first half of the code, it should ACCEPT that combination.

Now, my problem is, I have 2 lines of records in my txt file right now. So that should mean that in my datas named List, there should be 2 "items". The first line for example is this in the txt file: Example1;example123;1 And the second line is this: Example2;example234;1

Every time I write Example2 and example234 in the textboxes, it WORKS. Every time I write Example1 and example123 in the textboxes, it DOESNT WORK and I get the MessageBox message.

Anyone have any idea where did I go wrong?

I think that you are trying to do something along these lines? The ReadLine() will automatically move to the next row in the file.

if (File.Exists("example.txt"))
        {
            StreamReader sr = new StreamReader("example.txt");
            while(!sr.EndOfStream)
            {
                string[] data = sr.ReadLine().Split(';');
                v.text1 = data[0];
                v.text2 = data[1];
                v.int1 = Convert.ToInt32(data[2]);
                datas.Add(v);
            }
            sr.Close();
         }

To propose an additional improvement, use using to create the StreamReader and it will take care of the file handeling for you:

if (File.Exists("example.txt"))
    {
        using(StreamReader sr = new StreamReader("example.txt"))
        {
            while(!sr.EndOfStream)
            {
                string[] data = sr.ReadLine().Split(';');
                v.text1 = data[0];
                v.text2 = data[1];
                v.int1 = Convert.ToInt32(data[2]);
                datas.Add(v);
            }
        }
     }

(And maybe include the case that the file does not exist as an error and catch it.)

Remove your loop:

for(int j=0; j<x; j++)
{
    sr.ReadLine();
}

I am assuming you are attempting to position to the correct line, but StreamReader.ReadLine() already advances the read position. You don't need the loop.

What is happening is that your loop is reading past the end of the file, so then the ReadLine in

string[] data = sr.ReadLine().Split(';');

returns null, and so the Split() throws a null reference exception.

Your loop is the while. The for() loop will just disrupt the flow. My guess is you think you have to read from the start every time you want to do a ReadLine(). But the stream will remember where you left off after the last ReadLine().

    if (File.Exists("example.txt"))
    {
        StreamReader sr = new StreamReader("example.txt");
        while(!sr.EndOfStream)
        {
            string[] data = sr.ReadLine().Split(';');
            v.text1 = data[0];
            v.text2 = data[1];
            v.int1 = Convert.ToInt32(data[2]);
            datas.Add(v);
        }
        sr.Close();
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM