w3resource

C#: Create and write some line of text which does not contain a given string in a line

C# Sharp File Handling : Exercise-7 with Solution

Write a C# Sharp program to create and write some lines of text into a file. This does not contain a given string on a line.

Sample Solution:-

C# Sharp Code:

using System; // Importing the System namespace for basic functionalities
using System.IO; // Importing the System.IO namespace for file operations

class WriteTextFile // Declaring a class named WriteTextFile
{
    static void Main() // Declaring the Main method
    {
        string fileName = @"mytest.txt"; // Initializing a string variable with the file name
        string[] ArrLines; // Declaring a string array variable to store lines of text
        string str; // Declaring a string variable for input string to ignore
        int n, i; // Declaring variables for counting lines and iterating

        Console.Write("\n\n Create and write some line of text which does not contain a given string in a line  :\n"); // Displaying a message in the console
        Console.Write("------------------------------------------------------------------------------------------\n");

        if (File.Exists(fileName)) // Checking if the file already exists and deleting it if true
        {
            File.Delete(fileName); // Deleting the file if it exists
        }

        Console.Write(" Input the string to ignore the line : "); // Prompting the user to input a string
        str = Console.ReadLine(); // Reading the string to ignore

        Console.Write(" Input number of lines to write in the file  : "); // Prompting the user to input the number of lines
        n = Convert.ToInt32(Console.ReadLine()); // Reading the number of lines provided by the user
        ArrLines = new string[n]; // Initializing the string array with the provided number of lines

        Console.Write(" Input {0} strings below :\n", n); // Prompting the user to input the lines of text

        // Loop to input lines of text based on the user-defined count
        for (i = 0; i < n; i++)
        {
            Console.Write(" Input line {0} : ", i + 1); // Prompting for each line
            ArrLines[i] = Console.ReadLine(); // Reading and storing each line of text in the array
        }

        // Writing lines to the file, excluding those containing the given string to ignore
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"mytest.txt"))
        {
            foreach (string line in ArrLines)
            {
                if (!line.Contains(str)) // Writing the line to the file if it doesn't contain the specified string
                {
                    file.WriteLine(line);
                }
            }
        }

        using (StreamReader sr = File.OpenText(fileName)) // Opening a StreamReader to read the content of the file
        {
            string s = "";
            Console.Write("\n The line has been ignored if it contains the string '{0}'. \n", str); // Displaying a message about ignored lines
            Console.Write("\n The content of the file is  :\n"); // Displaying a message
            Console.Write("----------------------------------\n");

            // Reading and displaying each line of the file
            while ((s = sr.ReadLine()) != null) // Looping through each line until the end of the file
            {
                Console.WriteLine(" {0} ", s); // Displaying each line in the console
            }
            Console.WriteLine(); // Adding a newline for formatting
        }
    }
}

Sample Output:


 Create and write some line of text which does not contain a given string in a line  :                        
------------------------------------------------------------------------------------------                    
 Input the string to ignore the line : easy                                                                   
 Input number of lines to write in the file  : 2                                                              
 Input 2 strings below :                                                                                      
 Input line 1 : w3resource                                                                                    
 Input line 2 : it is easy tutorial                                                                           
    
 The line has ignored which contain the string 'easy'.                                                        

 The content of the file is  :                                                                                
----------------------------------                                                                            
 w3resource
 

Flowchart :

Flowchart: C# Sharp Exercises - Create and write some line of text which does not contain a given string in a line.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a program in C# Sharp to create a file and write an array of strings to the file.
Next: Write a program in C# Sharp to append some text to an existing file.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/csharp-exercises/file-handling/csharp-file-handling-exercise-7.php