CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2011
    Posts
    10

    Book for command-line application

    Are there any books about C sharp that only considers command-line applications?
    I am new to OOP and I only want to learn the concepts to do some command-line
    app for my school assignment. thanks

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Book for command-line application

    No need for a book. The internet is your best resource. This is a good introduction to the language: http://www.csharp-station.com/tutorial.aspx

    There's not much special for command line applications. Arguments passed on the command line (seperated by spaces) get put into the args argument of main(string[] args). You can figure out how many arguments by calling args.Length. Loop over them like:
    Code:
    for(int i = 0; i < args.Length; i++)
    {
        Console.WriteLine(args[i]); //Print the args to output... or whatever you want to do with them
    }
    You can read from the console like:

    Code:
    string someData = Console.ReadLine(); //User enters information until they push enter, in which case the string gets stored in the newly-declared someData variable.
    You can write to the console like:

    Code:
    Console.Write("Blah"); //Write blah to the console, without a new line
    Console.WriteLine("Blah"); //Write blah to the console, then add a new line
    Console.WriteLine("{0} is {1}", "programming", "fun"); //Write "programming is fun" to the console, then add a new line
    File IO can be done (for many files) most efficiently like (make sure you have the System.IO using directive):

    Code:
    string pathToYourFile = @"C:\mydata.txt";
    string[] fileLines = File.ReadAllLines(pathToYourFile);
    
    //Then you can loop over the lines of your file
    You can write messages to standard error (instead of standard output) like:

    Code:
    Console.Error.WriteLine("Error: You didn't get me any arguments!");
    You can have your application exit with an error code:

    Code:
    Environment.Exit(-1);
    Commonly:

    Code:
    //Check right number of arguments passed
    if( args.Length != 2 )
    {
        Console.Error.WriteLine("Error: Wrong number of arguments");
        Console.Error.WriteLine("Usage: Program.exe [First arg description] [Second arg description]");
        Environment.Exit(-1);
    }
    Anyway, hopefully that main link and some of these idioms will help you.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured