C# Fundamentals for Absolute Beginners: (12) while Iterations and Reading Data from a Text File

Sign in to queue

The Discussion

  • User profile image
    SalT1

    Hi Bob, I just wanted to express my appreciation for your excellent lessons! I started off on the previous version of this series (C# Visual Studio 2012) and then switched over to this version recently.

    I was confused by the null statements on lines 16 & 19, specifically what is the value of the variable line after ReadLine() returns a null after the last line in Values.txt? According to the definition I found below, wouldn't that make line = 0 ? And if so, isn't that a known and determinable ?

    "NULL is a built in constant which has a value of 0. It is also the same as the character '\0' used to terminate strings in C"

    Thanks,

    Confused

  • User profile image
    Dante

    I believe that statement is incorrect, since null is different than the value "0". Bob clear states that in the video, where he mentions that even an empty variable (like a declared int with no assigned value) is not a null variable. The end of a text file has a special code that we can read as a null. This way, you can read a text file full of "0" lines without having to worry for your method ending prematurely.

  • User profile image
    caresrg

    I'm new to all this, and at about 9 minutes in I'm trying to bring up the same Intellisense menu as Bob. Only 2 items show up, though: [Generate class for 'StreamReader'] and [Generate new type...]. How do I get [using System.IO;] or [System.IO.StreamReader] to show up? Thanks!

  • User profile image
    caresrg

    NVM. I misspelled the class. :/

  • User profile image
    maum2201

    thanks~ :)

  • User profile image
    MoCode

    Hi Bob. Just noticed my typo error why I was getting squiggly lines after the ! symbol.  But my question is, why is: (line != null) different from (line ! = null) or basically adding an extra space after ! makes the statement unrecognizable? I was under the impression that spaces don't really matter unless obviously its fallowed by a (dot)? Pardon my Einstein questions here:)

  • User profile image
    Adrian

    I like how Bob wrote down "The Numbers" from the TV-series Lost (4, 8, 15, 16, 23, 42). I like your style, Bob :)

  • User profile image
    tech4sho

    Dear Bob Tabor,

     In reference to my Properties Tab within my Values.txt file, my full path is C:  \Users\User\documents\visual studio2010\Projects\ReadTextFileWhile\ReadTextFileWhile\TextFile1.txt and I'm getting a FileNotFoundException was unhandled where it is saying it could nt find the file in my bin/Debug directory. I did like you said in the video and changed my Property window under Content with the drop down list from 'Do not copy' to 'Copy always'. but if my full path is different from the exception's location of where the TextFile should be, how can I resolve this situation?

  • User profile image
    tech4sho

    @tech4sho:As a default in my Solution Explorer window, instead of my file being named "Values.txt", as yours was in the video, mine was named "Textfile1.txt", which didn't allow the file to be copied correctly into the bin/Debug directory. Why that occurred, I'm not aware of. Anyway when I renamed the file in the Solutions Explorer window, the program ran.

  • User profile image
    Ryan

    Thanks Bob. This is awesome and i can't tell you how glad i am to have this resource.

  • User profile image
    Victor

    "4, 8, 15, 16, 23, 42" So you're a LOST fan as well, huh?

  • User profile image
    Trendkiller

    @Dante:Well you, just dispelled my doubts...

  • User profile image
    Juan Jose

    I followed along your lecture and when I try to debug and resolve the .txt window into one, I found three errors that did not let me continue programing. There was a squiggly line under the {} symbol. Incidentally when I was working I probably erased some of the brackets and later I tried to input them when I did not see them as you had them on your page. I could not finalize the operation.

  • User profile image
    BobTabor

    @Juan Jose: Could you post your code AND let me know which line / part you saw the red squigglies under?  Thank you.

  • User profile image
    Cloud

    Hi, congratulations for these video tutorials, they're the best ones I ever found about programming languages!

    There's only one thing I didn't really get: what exactly does the StreamReader.ReadLine() method? I kind of understood that a StreamReader value, when declared, takes one or more files to use as input, but I don't really get how he understands, during the while iterations, that he has to take the first line, then the second line, etc...

  • User profile image
    vahid

    hi bob. First, thank you for your great lessons. There is a little miss understanding with line of code number 20. Correct me if i'm wrong. There is no need to check again if the line is null or not. Because in line 17 we checked it already and if it was null it never get in the block at first place. So that if statement is unnecessary. Am I right?

  • User profile image
    Rashid

    Hello Bob, great video by the way. I do have one question. I wanted to you know if there is a sudden change to the program if you commented out 'myReader.Close()' on line 24. Can the program produce errors without that line active? Thanks.

  • User profile image
    Vahid

    @BobTabor. hi bob. First, thank you for your great lessons. There is a little miss understanding with line of code number 20. Correct me if i'm wrong. There is no need to check again if the line is null or not. Because in line 17 we checked it already and if it was null it never get in the block at first place. So that if statement is unnecessary. Am I right?

  • User profile image
    BobTabor

    @Vahid: Have you tried your solution?  ReadLine() spits out null when it reaches the end of the file:  https://msdn.microsoft.com/en-us/library/system.io.streamreader.readline%28v=vs.110%29.aspx ... you might be able to skip the loop altogether with ReadLines() (notice the 's') but I wanted to show you the while loop.

  • User profile image
    cooldude312

    I keep getting squiggly red lines here's my code:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace HelperMethod

    {

    class Program

    {

    static void Main(string[] args)

    {

    string myvalue = superSecretFormula("world);

    Console.WriteLine(myvalue);

    Console.ReadLine();

    }

     

    private static string superSecretFormula()

    {

    //some cool stuff cool stuff here

    return "Hello World!";

    }

    private static string superSecretFormula(string name);

    {

    return string.format("Hello{0}, name");

    }

     

    }

    }

     

  • User profile image
    cooldude312

    I keep getting squiggly red lines here's my code:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace HelperMethod

    {

    class Program

    {

    static void Main(string[] args)

    {

    string myvalue = superSecretFormula("world");

    Console.WriteLine(myvalue);

    Console.ReadLine();

    }

     

    private static string superSecretFormula()

    {

    //some cool stuff cool stuff here

    return "Hello World!";

    }

    private static string superSecretFormula(string name);

    {

    return string.format("Hello{0}, name");

    }

     

    }

    }

     

  • User profile image
    John

    cooldude312, your return string.format("Hello{0},name"); should be return string.format("Hello{0}", name);

  • User profile image
    wayne

    Hi Bob. I keep getting red squigglies under "Values.txt". The error message I get for it says that Argument 1: cannot convert from 'string' to 'System.IO.Stream'

    I am also getting an error on the Close() method on myReader that says StreamReader does not contain a definition for 'Close".

    Here's my code in case I am missing something....

    using System;
    using System.IO;

    namespace CSharpFundamentalsReadTextFileWhile
    {
    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(line);
    }
    myReader.Close();
    Console.ReadLine();
    }
    }
    }

Add Your 2 Cents