Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8483839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:12:59+00:00 2026-06-10T20:12:59+00:00

I am trying to learn the basics of C# using Sams Teach Yourself C#

  • 0

I am trying to learn the basics of C# using Sams Teach Yourself C# in 21 days.

I have created this program by copying it line by line from the day 1 type and run section. It compiles fine but when you run it, it gives this error: “Input string was not in the correct format”.

I am running the program from the console.

I am using the Visual Studio 2010 Express editor.

The code I copied is:

using System;
using System.IO;

/// <summary>
/// Class to number a listing. Assumes fewer than 1000 lines.
/// </summary>

class NumberIT
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>

    public static void Main(string[] args)
    {
        // check to see if a file name was included on the command line.

        if (args.Length <= 0)
        {
            Console.WriteLine("\nYou need to include a filename.");
        }
        else
        {
            // declare objects for connecting to files...
            StreamReader InFile = null;
            StreamWriter OutFile = null;

            try
            {
                // Open file name included on command line...
                InFile = File.OpenText(args[0]);

                // Create the output file...
                OutFile = File.CreateText("outfile.txt");
                Console.Write("\nNumbering...");

                // Read first line of the file...
                string line = InFile.ReadLine();
                int ctr = 1;

                // loop through the file as long as not at the end...
                while (line != null)
                {
                    OutFile.WriteLine("{1}: {2}", ctr.ToString().PadLeft(3, '1'), line);
                    Console.Write("..{1]..", ctr.ToString());
                    ctr++;
                    line = InFile.ReadLine();
                }
            }

            catch (System.IO.FileNotFoundException)
            {
                Console.WriteLine("Could not find the file {0}", args[0]);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
            finally
            {
                if (InFile != null)
                {
                    // Close the files
                    InFile.Close();
                    OutFile.Close();
                    Console.WriteLine("...Done.");
                }
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:13:01+00:00Added an answer on June 10, 2026 at 8:13 pm

    Your culprits here are the OutFile.WriteLine and Console.Write statements:

    OutFile.WriteLine("{1}: {2}", ctr.ToString().PadLeft(3, '1'), line);
    Console.Write("..{1]..", ctr.ToString());
    

    It should read:

    OutFile.WriteLine("{0}: {1}", ctr.ToString().PadLeft(3, '1'), line);
    Console.Write("..{0}..", ctr.ToString());
    

    Note that the placeholders in the format string start from 0. Your closing bracket on the second statement was a square bracket instead of a curly one.

    Another tip: you don’t need to call .ToString() on ctr in the latter case, unless you wanna specify a culture explicitly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn how to do XPath queries from Python using this sample
I'm trying to learn jquery, I've some basics of javascript. I've written this piece
I am trying to learn the basics of dependency injection (DI). To this end
I just started using PDO (loving it!). Still trying to learn the basics. Currently
I'm new to WCF, trying to learn the basics. I've created a simple C#
As part of my Personal Improvement Program (PIP™), I'm trying to learn the basics
I am trying to learn basic cursors using PostgreSQL. This is my script: DECLARE
I am trying to learn the basics of socket programming and am using some
Greetings, everyone. I'm trying to learn some Silverlight basics, and have decided to write
I have been trying to learn Java for the past few days so my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.