Appending To A Text File - C# Programming Exercise

In this exercise of C#, you need to create a program that asks the user for several sentences (until they just press Enter without typing anything) and stores those sentences in a text file named "sentences.txt". If the file already exists, the new content must be appended to the end of the file without overwriting the previously stored content. The program should properly handle the opening and closing of the file, ensuring that the sentences are written on new lines within the file.

This exercise is perfect for learning file manipulation in C#, specifically how to append content to an existing file using the StreamWriter class with the appropriate file-opening mode (append). It also teaches how to interact with the user continuously until they decide to stop entering data, while making sure the program doesn't overwrite existing data in the file.

 Category

File Management

 Exercise

Appending To A Text File

 Objective

Create a program to ask the user for several sentences (until they just press Enter) and store them in a text file named "sentences.txt". If the file exists, the new content must be appended to its end.

 Write Your C# Exercise

// Importing necessary namespaces to handle input/output functionalities
using System;
using System.IO;

public class Program
{
    public static void Main()
    {
        // Informing the user about the functionality of the program
        Console.WriteLine("Enter sentences (press Enter on an empty line to finish):");

        // Initializing a StreamWriter to append data to "sentences.txt" without overwriting
        using (StreamWriter writer = new StreamWriter("sentences.txt", append: true))
        {
            // Infinite loop to continuously gather sentences from the user
            while (true)
            {
                // Reading a sentence from the user's input
                string sentence = Console.ReadLine();

                // Checking if the input is empty to end the input gathering process
                if (string.IsNullOrEmpty(sentence))
                    break;  // Exiting the loop if no sentence is entered

                // Writing the user's sentence to the file, appending it to the end
                writer.WriteLine(sentence);
            }
        }

        // Informing the user that the sentences have been appended to the file
        Console.WriteLine("Sentences appended to sentences.txt.");
    }
}

 Share this C# exercise

 More C# Programming Exercises of File Management

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Display file contents

    In this exercise of C#, you need to create a program that displays all the contents of a text file on the screen. The file name will either be entered via the command...

  •  Extended TextToHTML (files)

    In this exercise of C#, you need to expand the TextToHtml class so that it can dump its result to a text file. You should create a ToFile method that ta...

  •  Logger

    In this exercise of C#, you need to create a Logger class with a static Write method, which will append a certain text to a log file. The method should ...

  •  More

    In this exercise of C#, you need to create a program that behaves like the Unix "more" command. The program should display the contents of a text file and prom...

  •  Text replacer

    In this exercise of C#, you need to create a program to replace words in a text file, saving the result into a new file. The program should take as parameters the fil...

  •  Count letters in a file

    In this exercise of C#, you need to create a program to count how many times a specific character appears inside a file of any kind. The file and the letter to search...