Hello all hope all is well, Im new to c# and new to the site so bare with a noob :). any way I have an assignment and Im running in to a problem where I don't know how to have c# read a text file and turn the numbers into the text file in to an array. To top it all off I need it to pass the parameters in to a text file. using python as a example i need it to look like this:

def readFile (filename):
    data = []
    source = file (filename)
    for line in source:
        words = line.split ()
        for word in words:
            data.append (int (word))
    source.close ()
    return data

I think I know how and/or where I can find the infor to .split and append it. and this is my febel attemp to try and do it in C#:

namespace Reaingfiles

{
    class program{


        public static void checkit(string file)
        int data[];
    {
        StreamReader src = new StreamReader(src);
        while(!src.EndOfStream)
        {
            string line = src.ReadLine();
            Console.WriteLine(line);

        }

    }


    static void Main()
    {
        checkit("data.txt");

    }
  }

}

im using system, system.i0, and system.text. I tried to make the data array, then I got lost again. But please help, thank you. and if this was a bad question please help me fine tune it.

Dani AI

Generated

provided a useful Python model and raised a good point about avoiding large in‑memory arrays. Two practical C# patterns solve this cleanly: (1) read everything into an int[] (simple, eager) or (2) expose a streaming IEnumerable<int> (lazy, low memory) that can be passed directly to other methods. Common pitfalls in the original snippet: the method signature and the new StreamReader(src) line are incorrect — use the filename when constructing the reader, and prefer int.TryParse to avoid exceptions on bad tokens.

Eager (returns an array):

static int[] ReadNumbersFromFile(string path)
{
    var list = new List<int>();
    foreach (var line in System.IO.File.ReadLines(path))
    {
        foreach (var token in System.Text.RegularExpressions.Regex.Split(line, @"\s+"))
        {
            if (int.TryParse(token, out int n))
                list.Add(n);
        }
    }
    return list.ToArray();
}

Lazy (returns an enumerable that can be passed around without forcing a full array):

static IEnumerable<int> ReadNumbers(string path)
{
    foreach (var line in System.IO.File.ReadLines(path))
        foreach (var token in System.Text.RegularExpressions.Regex.Split(line, @"\s+"))
            if (int.TryParse(token, out int n))
                yield return n;
}

// example caller that accepts either form
static void ProcessNumbers(IEnumerable<int> numbers)
{
    foreach (var value in numbers)
        Console.WriteLine(value);
}

Notes and troubleshooting: prefer File.ReadLines (streams the file). Use Regex.Split(..., @"\s+") or Split(..., StringSplitOptions.RemoveEmptyEntries) to handle arbitrary whitespace. Log or skip parse failures instead of throwing. For testability, accept a TextReader (so a StringReader can be injected in tests) or expose both ReadNumbers(TextReader) and ReadNumbers(string path). If the file is small and random access is needed, return an array; if size or latency is a concern, return IEnumerable<int> and let the caller decide when to materialize with ToArray().

Alternatively, I could just create an array from a file and pass the array as an argument, but I would rather not do that. The array wouldn't be that big, given the size of the text files, or I could convert the contents of the text file into a string and pass that. Either way, I want to avoid it.

I've tried mucking around with this a way to create a StreamReader Object, but I haven't found a way to pass that an argument to a function. If there's a way to do that, great!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.