|
-
September 3rd, 2012, 04:56 AM
#1
TYCSharp in 21 days - sample code issue
Hey all, newbie here who is trying to learn the basics of C#
I am currently going through Brad's TYCSharp in 21 days book and am having issues with the Type & Run program from Day 1 section.
I have copied the code line by line in to my editor (Visual Studio 2010 Express). It compiles without errors but when I run the program, I get this error: "Input string was not in the correct format".
Any help will be much appreciated as I don't want to move on to Day 2 with this program not running correctly.
The code is:
Code:
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.");
}
}
}
}
}
Last edited by Arjay; September 12th, 2012 at 01:50 PM.
Reason: Added code tags
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|