Reads an ASCII encoded file and writes the text to another file in wide character format
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Convert.cs -- Reads an ASCII encoded file and writes the text to another file
// in wide character format.
// Compile this program with the following command line:
// C:>csc Convert.cs
//
namespace nsConvert
{
using System;
using System.Text;
using System.IO;
public class Convert
{
static public int Main ()
{
// First, make sure both the input and output files can be opened
FileStream ostream;
FileStream istream;
try
{
istream = new FileStream ("Sample.asc", FileMode.Open, FileAccess.Read);
}
catch (Exception)
{
Console.WriteLine ("Cannot open Sample.asc for reading");
return (-1);
}
try
{
ostream = new FileStream ("Sample.wcs", FileMode.Create, FileAccess.ReadWrite);
}
catch (Exception)
{
Console.WriteLine ("Cannot open Sample.wcs for writing");
istream.Close ();
return (-1);
}
// Create a stream reader and attach the input stream with ASCII encoding
StreamReader reader = new StreamReader (istream, new ASCIIEncoding());
string str = reader.ReadToEnd ();
// Create a stream writer and attach the output stream using Unicode encoding
StreamWriter writer = new StreamWriter (ostream, new UnicodeEncoding());
// Write the text to the file.
writer.Write (str);
// Flush the output stream
writer.Flush ();
// Close the streams
ostream.Close ();
istream.Close ();
return (0);
}
}
}
//File: Sample.asc
/*
The quick red fox jumps over the lazy brown dog.
Now is the time for all good men to come to the aid of their Teletype.
Peter Piper picked a peck of peppered pickles.
*/
Related examples in the same category