user1712507 user1712507 - 7 months ago 26
Python Question

how to pass parameters for opening files C#

I am very new to c# and Im having issues trying to understanding this. How to pass a parameter to (or from I tend to confuse the terminology) the main function in order to read a text file? I have a python function that better shows what I want to do.

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 have a basic understanding of how to open files in c# but scouring the web I can't find anything that can help me do what I want or at least help translate. here's my basic understanding:

using System;
using System.Text;
using System.IO;

public class Readingfiles {


public static void Main()
{
StreamReader src = new StreamReader("Data.txt");

while(!src.EndOfStream)
{
string line = src.ReadLine();
Console.WriteLine(line);
}

}
}


Please help, If it helps, Im using sublime text and compiling on terminal through mcs/mono.

Ian Ian
Answer

You should have input argument args for your main():

static void Main(string[] args)

As an example:

class Program {
    static void Main(string[] args) {
        Console.WriteLine("This is the program");
        if (args == null || args.Length == 0) {
            Console.WriteLine("This has no argument");
            Console.ReadKey();
            return;
        }
        Console.WriteLine("This has {0} arguments, the arguments are: ", args.Length);
        for (int i = 0; i < args.Length; ++i)
            Console.WriteLine(args[i]);
        Console.ReadKey();
    }
}

The program will show you if you call the .exe with argument or not:

C:\Release> ConsoleApplication.exe //has no argument
C:\Release> ConsoleApplication.exe Data.txt //has one argument
C:\Release> ConsoleApplication.exe CallWith Data.txt //has two arguments
C:\Release> ConsoleApplication.exe "CallWith Data.txt" //has ONE arguments

In your case, likely the argument is just a single string, thus, putting the input checking on args[0] will be enough:

public static void Main(string[] args)
{
    if (args == null || args.Length == 0) {
        Console.WriteLine("Error: please specify the file to read!");
        Console.ReadKey();
        return;
    }

    try {

        StreamReader src = new StreamReader(args[0]);

        while(!src.EndOfStream)
        {
            string line = src.ReadLine();
            Console.WriteLine(line);
        }
    } catch (Exception ex) {
        Console.WriteLine("Error while reading the file! " + ex.ToString());
    }

    Console.ReadKey();    
}

Edit:

Your final solution should look like this (simply put the Main block to the correct namespace and class) and you should use all the usings:

using System;
using System.IO;

namespace ConsoleApplication2 {
    class Program {
        public static void Main(string[] args) {
            if (args == null || args.Length == 0) {
                Console.WriteLine("Error: please specify the file to read!");
                Console.ReadKey();
                return;
            }

            try {

                StreamReader src = new StreamReader(args[0]);

                while (!src.EndOfStream) {
                    string line = src.ReadLine();
                    Console.WriteLine(line);
                }
            } catch (Exception ex) {
                Console.WriteLine("Error while reading the file! " + ex.ToString());
            }

            Console.ReadKey();
        }
    }
}