- Course
- Text Files A
- Write to a text file
Write to a text file
Last updated:
8/23/2020
⁃
Difficulty:
Easy
Create a program in C# to request lines from the user and write them in a text file.
Type lines until the user presses Enter.
Input
dapibus
pretium
radar
Output
Solution
using System;
using System.IO;
public class WriteLinesTextFile
{
public static void Main(string[] args)
{
string fileName = "out.txt";
using (StreamWriter file = File.CreateText(fileName))
{
string line;
do
{
line = Console.ReadLine();
if (line.Length != 0)
{
file.WriteLine(line);
}
}
while (line.Length != 0);
}
}
}