0

I need to read data from a file, so I have this code.

public static void readData()
{
    TextReader readFile;
    string chain;
    bool found = false;
    string[] fields = new string[2];
    char[] breakUp = { ':' };

    try
    {     
        readFile = new StreamReader("C:\\Users\\unam\\Documents\\Hugo\\Datos.txt");

        chain = readFile.ReadLine();
        while (chain != null)
        {
            fields = chain.Split(breakUp);


            if (fields[0].Trim().Equals("Name"))
            {
                name = fields[1].Trim();            
            }
            else
            {
                if (fields[0].Trim().Equals("Age"))
                {
                    Age = fields[1].Trim();    
                }
            }
            readFile.Close();

         }
    }
    catch(FileNotFoundException fe)
    {
        Console.WriteLine("¡File not found!" + fe.Message);
    }
    catch(Exception e)
    {
        Console.WriteLine("¡File not found!" + e.Message);
    }
}         

And in the document I have

  • Name: Hugo
  • Age: 23

When I execute the code it stays in a loop in the first if so it just reads the name again and again, my question is, how can I get out of that loop to read the other data?

3
  • 1
    You need to keep reading lines into chain. Don't let it stay the same value or nothing changes. Jun 20, 2019 at 17:28
  • chain = readFile.ReadLine(); Should be in while section Jun 20, 2019 at 17:29
  • 2
    Also, you should be wrapping your StreamReader/TextReader in a using block. Jun 20, 2019 at 17:39

2 Answers 2

2

You need to update the string you read each time:

       chain = readFile.ReadLine();
       while (chain != null)
       {
           fields = chain.Split(breakUp);


           if (fields[0].Trim().Equals("Name"))
           {
                name = fields[1].Trim();
           }
           else
           {
               if (fields[0].Trim().Equals("Age"))
               {
                    Age = fields[1].Trim();
               }
            }
       chain = readFile.ReadLine(); // <--
       }
       readFile.Close();

As pointed out by Broots Waymb you can put the read into the while-condition as well:

       while ((chain = readFile.ReadLine()) != null)
       {
           fields = chain.Split(breakUp);


           if (fields[0].Trim().Equals("Name"))
           {
                name = fields[1].Trim();
           }
           else
           {
               if (fields[0].Trim().Equals("Age"))
               {
                    Age = fields[1].Trim();
               }
            }
       }
       readFile.Close();
2
  • You can simplify this a little bit by doing while ((chain = readFile.ReadLine()) != null) and removing the 2 chain = readFile.ReadLine(); lines. Jun 20, 2019 at 17:37
  • I tried to make as little changes to the code as possible, but as this is a good improvement, I'll add it Jun 20, 2019 at 17:38
0

YOu need to check when you have reached to the end of the file, refer Docs

             using (StreamReader sr = new StreamReader(path)) 
            {
                while (sr.Peek() >= 0) 
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.