File input/output: Difference between revisions

Content added Content deleted
(awk)
Line 197:
 
=={{header|C sharp|C #}}==
<lang csharp>using System;
{{works with|C sharp|C #|1.0+}}
using System.IO;
This version will change line terminators if the input file doesn't use CrLf.
 
namespace FileIO
using System;
{
using System.IO;
class Program
namespace FileIO
{
classstatic Programvoid Main(string[] args)
{
static void Main(string[] args)try
{
ifusing (File.ExistsStreamReader reader = new StreamReader("input.txt"))
using (StreamWriter writer = new StreamWriter("output.txt"))
{
TextReaderstring trs = Filereader.OpenTextReadLine("input.txt");
TextWriterwhile tw(s != new StreamWriter(File.OpenWrite("output.txt")null);
while (tr.Peek() != -1)
{
string line = trwriter.ReadLineWriteLine(s);
while (tr.Peek() ! s = -1reader.ReadLine();
tw.WriteLine(line);
}
tw.Close();
tr.Close();
}
else
{
Console.WriteLine("Input File Missing.");
}
}
catch (Exception exception)
else{
twConsole.WriteLine(lineexception.Message);
}
}
}
}</lang>
 
There is an easier way in .NET 2.0:
 
<lang csharp>using System;
using System.IO;
 
namespace FileIO
{
class Program
{
classstatic Programvoid Main(string[] args)
{
static void Main(string[] args)try
{
if File.WriteAllText("output.txt", File.ExistsReadAllText("input.txt"));
{}
catch (Exception exception)
File.WriteAllText("output.txt", File.ReadAllText("input.txt"));
}{
elseConsole.WriteLine(exception.Message);
{
Console.WriteLine("Input File Missing.");
}
}
}
}
}</lang>
 
=={{header|C++}}==