-2

So I am just starting out C# with little to no knowledge, so this is more for learning for me than practical use. Therefore what I really would like to know is how I can get my code to work my way, even if there is a much simpler/quicker/smarter solution.

So what I wanna do is create a string array, and using a loop read in each line from a text file into a corresponding element of the array. That's what I tried to do here, and I would love to hear what solutions you have for this.

{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader ki = new StreamReader("kiserlet.txt");
            string[] a = new string[15];
            Console.ReadLine();
            int y = 0;

            int n = 0;
            for (int i = 0; i > 15; i++)
            {
                a[n] = Convert.ToString(ki.ReadLine());
                n++;
            }
            for (int x = 0;x > 15;x++)
            {

                Console.WriteLine(a[y]);
                y++;
            }
            Console.ReadLine();
            ki.Close();
        }
    }
}
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Botond Horváth
  • 55
  • 1
  • 1
  • 6
  • why is the array 15 in length? what if the txt being read has more than 15 lines? – d.moncada Nov 19 '17 at 23:10
  • 1
    Your loops are wrong. You need I < 15. You are using > symbol. – Andrew Truckle Nov 19 '17 at 23:13
  • Damn I am dumb, thanks a lot , fixed my code immediately :) – Botond Horváth Nov 19 '17 at 23:16
  • [Google](https://msdn.microsoft.com/da-dk/library/ezwyzy7b(v=vs.110).aspx) is your [friend](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-read-a-text-file-one-line-at-a-time) – Kent Kostelac Nov 19 '17 at 23:21
  • Your 10 posts have 11 answers but only one ever has been accepted. Accepting answers (and later upvoting posts) helps other users find good posts and is a way you can help others even if you are not in a position to post answers. The very brief [tour] explains how SO works. – Ňɏssa Pøngjǣrdenlarp Nov 19 '17 at 23:39
  • Why is everyone answering this question? It has clearly been answered many times before; here's an example: https://stackoverflow.com/questions/4220993/c-sharp-how-to-convert-file-readlines-into-string-array – twoleggedhorse Nov 20 '17 at 00:08
  • You'll see, in the linked question, a character encoding is specified. When reading a text file, you must use the encoding that was used to write it. – Tom Blodget Nov 20 '17 at 01:23

1 Answers1

6

You can read each line of the file into an array, then iterate through it.

class Program
{
    static void Main(string[] args)
    {
        // this will read all lines from within the File
        // and automatically put them into an array
        //
        var linesRead = File.ReadLines("kiserlet.txt");

        // iterate through each element within the array and
        // print it out
        //
        foreach (var lineRead in linesRead)
        {
            Console.WriteLine(lineRead);
        }
    }
}
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • 1
    It might be better to use `ReadLines` - especially if the file is large - as it returns lines lazily. – Enigmativity Nov 19 '17 at 23:34
  • 10k rep and you didn't flag this as a duplicate? https://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled – twoleggedhorse Nov 20 '17 at 00:10
  • 1
    Good answer. But sometimes we need to teach someone why what they did doesn’t work. In his case his loops used the > instead of < test. When I was at college I had to learn to use the hand held carpentry tools to learn the mechanics and principles before I could use the electric tools. – Andrew Truckle Nov 20 '17 at 05:23