File input/output: Difference between revisions
Content added Content deleted
(awk) |
|||
Line 197: | Line 197: | ||
=={{header|C sharp|C #}}== |
=={{header|C sharp|C #}}== |
||
⚫ | |||
{{works with|C sharp|C #|1.0+}} |
|||
⚫ | |||
This version will change line terminators if the input file doesn't use CrLf. |
|||
⚫ | |||
⚫ | |||
{ |
|||
⚫ | |||
class Program |
|||
⚫ | |||
{ |
{ |
||
static void Main(string[] args) |
|||
{ |
{ |
||
try |
|||
{ |
{ |
||
using (StreamReader reader = new StreamReader("input.txt")) |
|||
using (StreamWriter writer = new StreamWriter("output.txt")) |
|||
{ |
{ |
||
string s = reader.ReadLine(); |
|||
while (s != null) |
|||
⚫ | |||
{ |
{ |
||
writer.WriteLine(s); |
|||
⚫ | |||
⚫ | |||
} |
} |
||
tw.Close(); |
|||
tr.Close(); |
|||
⚫ | |||
⚫ | |||
{ |
|||
Console.WriteLine("Input File Missing."); |
|||
} |
} |
||
⚫ | |||
catch (Exception exception) |
|||
⚫ | |||
⚫ | |||
} |
} |
||
} |
} |
||
} |
} |
||
}</lang> |
|||
There is an easier way: |
There is an easier way in .NET 2.0: |
||
<lang csharp>using System; |
|||
using System.IO; |
|||
namespace FileIO |
|||
{ |
|||
class Program |
|||
{ |
{ |
||
static void Main(string[] args) |
|||
{ |
{ |
||
try |
|||
{ |
{ |
||
File.WriteAllText("output.txt", File.ReadAllText("input.txt")); |
|||
} |
|||
catch (Exception exception) |
|||
File.WriteAllText("output.txt", File.ReadAllText("input.txt")); |
|||
{ |
|||
Console.WriteLine(exception.Message); |
|||
{ |
|||
Console.WriteLine("Input File Missing."); |
|||
} |
|||
} |
} |
||
} |
} |
||
} |
} |
||
}</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |