Read a File and Display Contents in C++ Console App

C# has a wide array of file operations. These operations include opening a file, reading or writing to a file. There can be instances wherein you lot want to work with files directly, in which case you would use the file operations available in C#. Some of the basic file operations are mentioned below.

  1. Reading – This performance is the basic read operation wherein information is read from a file.
  2. Writing – This operation is the basic write performance wherein data is written to a file. By default, all existing contents are removed from the file, and new content is written.
  3. Appending – This operation also involves writing information to a file. The but difference is that the existing data in a file is not overwritten. The new data to be written is added at the end of the file.

In this tutorial, you lot will learn-

  • File.Exists
  • File.ReadAlllines
  • File.ReadAllText
  • File.Copy
  • File.Delete

Basics I/O Commands

C# and .Net tin can piece of work with files with the help of several File I/O commands. Let's accept a expect at some of these commands. For our example, we will assume that we have a file in the D bulldoze called Case.txt.
The file will be a unproblematic text file and have 2 lines equally shown below

  • Guru99 – .Net
  • Guru99 -C#

For our example, nosotros volition create a uncomplicated Console application and work with our File I/O commands. The console awarding is the bones 1 which was created in the earlier tutorial. In the console application, all code is written to the program.cs file.

File.Exists

The File exists method is used to check if a particular file exists. Then at present let's run across the lawmaking which can be used to check if our Example.txt file exists or not. Enter the below code in the program.cs file.

C# File Operations

using System; using System.Collections.Generic; using Organization.IO; using System.Linq; using System.Text; using Organization.Threading.Tasks; namespace DemoApplication {  form Tutorial  {   static void Master(string[] args)   {    String path = @"D:\Instance.txt";        if (File.Exists(path))    {     Panel.WriteLine("File Exists");    }    Console.ReadKey();   }  } }

Code Explanation:-

  1. Commencement, we are setting a cord variable with the path to our Instance.txt file.
  2. Next, nosotros use the File.Exists method to check if the file exists or not. If the File exists, a true value will be returned.
  3. If we get a true value and the file does be, then we write the bulletin "File Exists" to the console.

When the above code is fix, and the project is executed using Visual Studio, you volition get the below output.

Output:-

C# File Operations

From the to a higher place output, y'all can see that the File.Exists command was executed successfully, and the correct message was displayed in the console window.

File.ReadAlllines

The method is used to read all the lines ane by ane in a file. The lines are then stored in a string array variable. Let'south look at an example. Enter the below code in the program.cs file.

C# File Operations

using System; using System.Collections.Generic; using System.IO; using System.Linq; using Organization.Text; using Organization.Threading.Tasks; namespace DemoApplication {  class Tutorial  {   static void Principal(string[] args)   {    String path = @"D:\Example.txt";     String[] lines;    lines = File.ReadAllLines(path);     Console.WriteLine(lines[0]);    Console.WriteLine(lines[1]);     Console.ReadKey();   }  } }

Code Caption:-

  1. Offset, we are declaring a cord array variable. This will be used to store the result which volition exist returned by the File.ReadAllLines method.
  2. Next, we use the File.ReadAllLines method to read all the lines from our text file. The result is then passed to the lines variable.
  3. Since we know that our file contains just 2 lines, we can access the value of the array variables via the lines[0] and lines[one] command.

When the above code is set, and the project is run using Visual Studio, you will get the below output.

Output:-

C# File Operations

From the output, yous can see that the File.ReadAllLines command returned both the lines from our file Example.txt

File.ReadAllText

This method is used to read all the lines in a file at once. The lines are and then stored in a string variable. Let's await at an example. Enter the below lawmaking in the plan.cs file.

C# File Operations

using Organization; using System.Collections.Generic; using Organisation.IO; using System.Linq; using Organization.Text; using System.Threading.Tasks; namespace DemoApplication {   form Tutorial   {    static void Main(string[] args)    {     String path = @"D:\Case.txt";      Cord lines;     lines = File.ReadAllText(path);     Console.WriteLine(lines);       Console.ReadKey();    }   } }

Code Explanation:-

  1. First, we are declaring a cord variable called Lines. This volition exist used to store the result which volition be returned past the File.ReadAllText method.
  2. Next, we use the File.ReadAllText method to read all the lines from our text file. The event is then passed to the lines variable.
  3. We can direct apply the Console.Writeline method to display the value of the Lines variable.

When the above code is gear up, and the project is run using Visual Studio, y'all volition get the below output.

Output:-

C# File Operations

From the output, you can come across that the File.ReadAlltext control returned both the lines from our file Example.txt

File.Copy

The method is used to brand a copy of an existing file. Permit's look at an case. Enter the below code in the program.cs file.

C# File Operations

using Arrangement; using System.IO; using System.Linq; using Organisation.Text; using Organisation.Threading.Tasks; namespace DemoApplication {   class Tutorial   {    static void Chief(cord[] args)    {    Cord path = @"D:\Instance.txt";     String copypath = @"D:\ExampleNew.txt";     File.Re-create(path,copypath);     Panel.ReadKey();    }   } }

Lawmaking Caption:-

  1. First, we are declaring a string variable called path. This will be the location of our Case.txt file. This file will exist the source file used for the copy operation.
  2. Adjacent, we are declaring a string variable called copypath. This will be the location of a new file called ExampleNew.txt file. This will be the destination file in which the contents will exist written from the source file Example.txt.
  3. Nosotros so call the File.Copy method to copy the file Example.txt file to the file ExampleNew.txt.

When the above lawmaking is set, and the project is run using Visual Studio, the file Example.txt volition exist copied to ExampleNew.txt.

File.Delete

The method is used to delete an existing file. Let'south await at an case. Enter the beneath code in the program.cs file.

C# File Operations

using Arrangement; using System.IO; using System.Linq; using System.Text; using Organisation.Threading.Tasks; namespace DemoApplication {   class Tutorial   {    static void Main(string[] args)    {    Cord path = @"D:\Instance.txt";     File.Delete(path);     Console.ReadKey();    }   } }

Code Explanation:-

  1. First, we are declaring a string variable called path. This volition be the location of our Instance.txt file. This is the file which volition be deleted.
  2. Next, we are calling the File.Delete method to delete the file.

When the above code is set, and the project is run using Visual Studio, the file Case.txt will be deleted from the D drive.

Summary

  • C# has a number of File operations which can be performed on files. Most of these operations are part of the class File.
  • If you desire to read data from a file, yous can use the File.ReadAlltext or File.ReadAllLines methods.
File Method Description
File.Exists File exists method is used to check if a particular file exists.
File.ReadAlllines The method is used to read all the lines one by ane in a file.
File.ReadAllText This method is used to read all the lines in a file at once.
File.Copy The method is used to make a copy of an existing file.
File.Delete The method is used to delete an existing file.

mcmillanalaingleuted.blogspot.com

Source: https://www.guru99.com/c-sharp-file-operations.html

0 Response to "Read a File and Display Contents in C++ Console App"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel