Artomes
Artomes

Reputation: 3

How to Stream string data from a txt file into an array


I'm doing this exercise from a lab. the instructions are as follows

This method should read the product catalog from a text file called “catalog.txt” that you should create alongside your project. Each product should be on a separate line.Use the instructions in the video to create the file and add it to your project, and to return an array with the first 200 lines from the file (use the StreamReader class and a while loop to read from the file). If the file has more than 200 lines, ignore them. If the file has less than 200 lines, it’s OK if some of the array elements are empty (null).

I don't understand how to stream data into the string array any clarification would be greatly appreciated!!

    static string[] ReadCatalogFromFile()
    {
        //create instance of the catalog.txt
        StreamReader readCatalog = new StreamReader("catalog.txt");

        //store the information in this array
        string[] storeCatalog = new string[200];
        int i = 0;

       //test and store the array information
        while (storeCatalog != null)
        {

            //store each string in the elements of the array?
            storeCatalog[i] = readCatalog.ReadLine();
            i = i + 1;
            if (storeCatalog != null)
            {
                //test to see if its properly stored

                Console.WriteLine(storeCatalog[i]);
            }
        }
        readCatalog.Close();
        Console.ReadLine();
        return storeCatalog;
    }

Upvotes: 0

Views: 1527

Answers (3)

Alexander Derck
Alexander Derck

Reputation: 14488

A for-loop is used when you know the exact number of iterations beforehand. So you can say it should iterate exactly 200 time so you won't cross the index boundaries. At the moment you just check that your array isn't null, which it will never be.

using(var readCatalog = new StreamReader("catalog.txt"))
{
  string[] storeCatalog = new string[200];

  for(int i = 0; i<200; i++)
  { 
    string temp = readCatalog.ReadLine();
    if(temp != null)
      storeCatalog[i] = temp;
    else 
      break;
  }

  return storeCatalog;
}

As soon as there are no more lines in the file, temp will be null and the loop will be stopped by the break. I suggest you use your disposable resources (like any stream) in a using statement. After the operations in the braces, the resource will automatically get disposed.

Upvotes: 0

Matthew Whited
Matthew Whited

Reputation: 22433

static string[] ReadCatalogFromFile()
{
    var lines = new string[200];
    using (var reader = new StreamReader("catalog.txt"))
        for (var i = 0; i < 200 && !reader.EndOfStream; i++)
            lines[i] = reader.ReadLine();
    return lines;
}

Upvotes: 0

Danny_ds
Danny_ds

Reputation: 11406

Here are some hints:

int i = 0;

This needs to be outside your loop (now it is reset to 0 each time).

In your while() you should check the result of readCatalog() and/or the maximum number of lines to read (i.e. the size of your array)

Thus: if you reached the end of the file -> stop - or if your array is full -> stop.

Upvotes: 2

Related Questions