c sharp

C# Write to File

Files are used to store data either manually or technically. File handling through the programming language refers to several operations like creating, opening , writing , reading the file, and etc. But the two basic operations that are done on the file are writing and reading. These are accomplished through the two functions: writeAlltext() and ReadAllText().

# WriteAlltext()

This is the function that is used to write the text into the file. writeAllText() and WriteAllLines() are two methods that slightly differ depending on the input text. These two functions create a new file at the specified location. But if the file is already present at the same location, then the text present inside the file is overwritten. We have used these methodologies to explain the writing text to file.

Example 1

To implement the phenomenon of writing to the file, we first provide the path to create a new file in which we want to write. In the program, two basic libraries of c sharp are used. The system library includes all the information regarding classes, data structures, arrays, etc,. The second one is system.IO which deals with the input and output streaming just like the iostream in C++. This library is essential for displaying data on the console from the file and taking the text into the file.

Using System;

Using System.IO;

Inside the class, and further in the main program, provide the path you want to create a new sample file. A string variable is declared here to store the directory of the file.

# String file = @"M:\Doccuments\samplefie.txt";

By using this file at this particular location, it will be created at the time of execution. We will see that file in the folder later on.

We will now start writing text to the file. For this purpose, a string-type variable will be declared which will contain the string of characters we want to write on the file.

# String text = "Hello.";

This string will be written through a built-in function in C sharp to add the text to the file. This function is writeAlltext(). It opens the file firstby following the path. Then it writes the line we have added through the variable. It takes two arguments, a file in which you want to add the text by opening it, and the second one is the text present in a string variable. As it is a built-in function, so it is called by the file.

# File.WriteAllText(file, text);

Now, if we want to display all the contents of the file on the console, we will use the ReadAllText method. Contrary to the writeAllText method, this function takes the value, in other words, it only reads the text not modifies it. It is also called by the file and takes the file as a parameter. The console statement helps to display the content.

# Console.Writeline(File.ReadAllText(file));

Unlike the previous write-to-file string, we can also add contents line by line to the file. For instance, we want to add 3 lines collectively at a time in the file Then we will declare an array of strings to store the text character by character. After this declaration, assign the values to an array. Then, we will again use the WriteAllText function to write all these lines in the file having the file and the array of strings as a parameter.

There is another method other than writeAllText() that also writes to the file. this is a StreamWriter which can add text line by line in the file. We can also declare a string variable and then provide the text in two lines separately. StreamWriter is activated by using a new keyword. We have created a stream writer of ‘writer’ names.

# Using(StreamWriter writer = new StreamWriter(file))

It will take a file in the parameter. Inside it, each loop is used, as it is an array of the string so it will contain all the characters. This loop will iterate throughout the string array, to display all characters present in specific lines.

The iteration will be through an object ‘ln’. Now, we will save this file of source code with the .cs extension that will open the file and write data in it with the extension of .txt.

For the execution of the filing system in the Linux terminal, we have used a compiler MCS that will compile the code Then leading towards Mono, it will execute the file while making the extension from .cs to .exe.

On the execution, you will see that the simple string that we write at the start is displayed. Thhen, the string array having three lines is displayed collectively. After that, those lines that are written through the stramWriter are also displayed here. To see the created file, we will go towards the documents following the path. Here you can see that a file is created with the name ‘sample’.

When the file opens, all the text will appear.. The text is written in the cs file but it is present in the txt file. This is because of the WriteAlltext and streamWriter functions.

Example 2

This is another example of writing to the file. But this time, the input is not directly assigned to the variable in the source code, but it is taken through the user. So first we will create a file sample.txt through streamwriter with SW object.

# StreamWriter SW = new streamWriter("/home/aqsayasin/sample.txt");

By providing the path, a file will be created with this name. Then, with the help of the console.writeline statement, we will ask the user to enter values so that the text can be added to the file.

# String str = console.Readline();

This method will be used to read the input added by the user and this is stored in the string variable. Through the object of streamWriter, we will write a line in the buffer by using the string object.

# Sw.writeLine(str);

Similarly, the line will be written in the output stream. Then, the stream will be closed, as we have added the text to the file.

# Sw.flush();

# Sw.close();

Leading to the main program, we will create an object of the writeTofile class to access the function dynamically. Then the function data is called through it.

# witeToFile wr = new writeToFile ();

Save the code and execute it. You will observe that, after the message is displayed, the user will write the text he wants to add.

To see the text entered by the user in the file, go to the Documents folder. Here you will see the sample.txt file.

Now open this file, and you will see the text.

Conclusion

‘Write to String’ in C sharp is implemented in the Linux operating system. We needed to create two files: one for the source code and the other by using the source code, to create a text file. Two basic methods are used to enter and read the text that are present in the file streaming of the file handling in any programming language. Both functions are used in the example to elaborate their working off writing and reading. Furthermore, another function streamwriter helps in achieving the same goal.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.