0

I want to have an array based on the lines in the file, but at the moment its a fixed sized array:

string[] converList = new string[6]; // Array containing TXT lines

Reading the file:

void ReadConver()
    {
        string line;
        int i = 0;

        System.IO.StreamReader file =
            new System.IO.StreamReader("C:\\Users\\Kennyist\\Documents\\Visual Studio 2010\\Projects\\soft140as3\\convert.txt");
        while ((line = file.ReadLine()) != null)
        {
            converList[i] = line;
            i++;
        }
    }

How would I do this?

3 Answers 3

5

You could create a list then use ToArray to make it into an array:

var cList = File.ReadAllLines("C:\\Users\\Kennyist\\Documents\\Visual Studio 2010\\Projects\\soft140as3\\convert.txt").ToList();
string[] converlist = clist.ToArray();

Also, use (@'C:\Kennyist...') instead of the double backslashes

Sign up to request clarification or add additional context in comments.

2 Comments

Well that was overly simple, Found something like that on google but I got an error with it before. Thanks!
The result of ReadAllLines already is an array. No need to use the LINQ ToArray extension method on it.
2

Instead of doing things the hard way, you can just use:

var arrTextLines = File.ReadAllLines(@"C:\Users\Kennyist\Documents\Visual Studio 2010\Projects\soft140as3\convert.txt");

arrTextLines will be an object with the type string[].

Comments

-1

Edited answer:

  string[] converList;
  System.IO.StreamReader file =
  new System.IO.StreamReader("C:\\Users\\Kennyist\\Documents\\Visual Studio 2010\\Projects\\soft140as3\\convert.txt");


  converList = new string[] { file.ReadToEnd() };

Thanks @Cole

6 Comments

How does this answer the OP's question at all? He wants to read a file into an array. This just creates an empty array of Length 0.
You don't understand .NET, do you? new T[] creates an array of size 0.
@ColeJohnson oh, my wrong, I understand now, Sorry, i'm new to generics functions and I use List<T> and not array maybe it'll work if it will be string[] converList;
I will edit my answer @ColeJohnson I now get what array is :D thank you :)
If you just use string[] converList;, you just did the equivalent of string[] converList = null.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.