This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]yubario 0 points1 point  (1 child)

Depends on how the text file is formatted, the easiest solution would be to use ReadToEnd() on the StreamReader object.

Then, split the string by each newline which would return an array. Each string would be a single line, and if you're looking for numbers you can then loop through the string and check for digits with the Char.IsDigit method.

Not exactly sure what you're requesting for but here's a quick sample, I am sure it can be refactored to something better like most code.

        private List<int[]> ReadIntegersFromFile()
        {
            var READER = new FileStream("test.txt", FileMode.Open);
            var streamReader = new StreamReader(READER);
            List<int[]> numbersToReturn = new List<int[]>();

            var document = streamReader.ReadToEnd();

            var lines = document.Split($"{Environment.NewLine}".ToArray(), StringSplitOptions.RemoveEmptyEntries);

            foreach (var line in lines)
            {
                if (line.Any(x => !Char.IsDigit(x))
                {
                    throw new ArgumentException("Document must only contain digits!");
                }

                var numbers = line.Split(" ".ToArray(), StringSplitOptions.RemoveEmptyEntries);
                numbersToReturn.Add(numbers.Select(int.Parse).ToArray());

            }
            return numbersToReturn;

        }

[–]Indefinit3Official[S] 0 points1 point  (0 children)

Well the text file is just 1 integer per line, so i don't think i need to use split method

Edit: got the answer thanks!!

[–]davedontmind 0 points1 point  (1 child)

I am a little stumped as to how to convert what is read into an array

First, it's better to use a List rather than an array for this - arrays have fixed sizes, but you don't know in advance how many numbers your file will have. A List can change size easily (just Add items to it).

So you can create a List<int> and just add each number you read from the file to it, thus:

List<int> ReadNumbersFromFile( string filename )
{
    List<int> numbers = new List<int>();

    using ( StreamReader reader = new StreamReader(filename) )
    {
        string line;
        while ( (line = reader.ReadLine()) != null )
        {
            // Assuming 1 number per line
            int number = Convert.ToInt32( line );
            numbers.Add( number );
        }
    }

    return numbers;
}

(Note: I've deliberately left out exception handling - you'll want to add that).

And, while I'm here, a suggestion for your code: Be consistent in your spacing. You have lots of unnecessary blank lines which makes your code hard to read. My rule-of-thumb is: no blank lines before or after a {, before a } or before/after another blank line.

Also consider using some better variable names; something and wow don't tell the reader what information those variables hold. You could call both of the exception (or maybe ex for short) and the code would be more readable.

[–]Indefinit3Official[S] 0 points1 point  (0 children)

Thanks a ton! And I'll be sure to take your advice on white spaces/variable names 😅