C# Program to Read a Text File

This is a C# Program to read the contents of the file.

Problem Description

This C# Program Reads the Contents of a File.

Problem Solution

It uses the library functions to read the data from the file that is created already in the same path of the program that is written.

Program/Source Code

Here is source code to Read the Contents of a File.The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Read Contents of a File
 */
using System;
using System.IO;
class FileRead
{
    public void readdata()
    {
        FileStream fs = new FileStream("Myfile.txt", FileMode.Open, FileAccess.Read);
        //Position the File Pointer at the Beginning of the File
        StreamReader sr = new StreamReader(fs);
        //Read till the End of the File is Encountered
        sr.BaseStream.Seek(0, SeekOrigin.Begin);
        string str = sr.ReadLine();
        while (str != null)
        {
            Console.WriteLine("{0}", str);
            str = sr.ReadLine();
        }
        //Close the Writer and File
        sr.Close();
        fs.Close();
    }
    public static void Main(String[] args)
    {
        FileRead fr = new FileRead();
        fr.readdata();
    }
}
Program Explanation

This C# program is used to read the contents of a file. It uses the library functions to read the data from the file that is created already in the same path of the program that is written. The ‘StreamReader’ is used to position the file pointer at the beginning of the file.

advertisement
advertisement

Then BaseStream.Seek() function is used to read till the end of the file is encountered. Using while loop check the condition that the value of ‘str’ variable is not equal to null. If the condition is true, then execute the iteration of the loop. Using ReadLine() function we are reading the content in the file and we are close the writer and file.

Runtime Test Cases
 
The text which your are reading are read from the file 
named myfile.txt that is created already.

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.