Gaboik
Gaboik

Reputation: 75

How can I print values from a text file in the console?

I've been trying to read some values from a Values.txt file and then print them in the console using C#. Everything appears to work. I've debugged the code and found nothing wrong and the program is compiling. The problem is that the values wont appear on the console. It just prints empty lines.

Here's my code :

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestFileReadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader myReader = new StreamReader("Values.txt");
            string line = "";

            while (line != null)
            {
                line = myReader.ReadLine();
                if (line!= null)
                    Console.WriteLine();
            }
            myReader.Close();
            Console.WriteLine("Allo");
            Console.ReadLine();
        }
    }
}

I'm using Visual Studio Express 2013

Upvotes: 0

Views: 1759

Answers (2)

David
David

Reputation: 219057

Nowhere do you actually print the values to the console.

You print an empty line here:

Console.WriteLine();

You probably meant to print the line variable:

Console.WriteLine(line);

Upvotes: 1

erikscandola
erikscandola

Reputation: 2946

You forgot to add variable lineto Console.WriteLine():

while (line != null)
{
    line = myReader.ReadLine();
    if (line!= null)
        Console.WriteLine(line);
}

Upvotes: 0

Related Questions