- Course
- Text Files A
- Appending to a text file
Appending to a text file
Last updated:
8/23/2020
⁃
Difficulty:
Easy
Create a C# program that asks the user for lines until you press Enter.
The lines should be updated at the end of the text file if it exists, but a new file should be created.
Input
dapibus
pretium
radar
Output
Solution
using System;
using System.IO;
public class AppendingTextFile
{
public static void Main(string[] args)
{
string fileName = "out.txt";
using (StreamWriter file = File.AppendText(fileName))
{
string line;
do
{
line = Console.ReadLine();
if (line.Length != 0)
{
file.WriteLine(line);
}
}
while (line.Length != 0);
}
}
}