C# - Reading Lines From a File Until the End of File is Reached
Last Updated :
27 Dec, 2021
Given a file, now our task is to read lines from the file until the end of the file using C#. Before reading the lines from a file we must have some data, so first we need an empty file in our path and then insert lines in our file and then we read lines from a file. So to do this task we use two basic operations that are reading and writing. The file becomes a stream when we open the file for writing and reading, here the stream means a sequence of bytes that is used for communication. So to our task, we use:
Path: For reading a file from any source we have to need the location/path. A Path is a string that includes a file path in a system.
@"c:\folder\file_name.txt"
We will check if the file exists in the path or not by using File.Exists(path) method
StreamWriter: StreamWriter is used to write a stream of data/lines to a file.
StreamWriter sw = new StreamWriter(myfilepath)
StreamReader: StreamReader is used to read a stream of data/lines from a file.
StreamReader sr = new StreamReader(myfilepath)
Peek: Used to read the data/lines from the file till the end of the file.
StreamReaderObject.Peek()
So all are placed in try() block to catch the exceptions that occur.
Example: Consider the path and file are:
C#
// C# program to read lines from a file
// until the end of file is reached
using System;
using System.IO;
class GFG{
public static void Main()
{
// File name is data
// File path is the following path
string myfilepath = @"c:\sravan\data.txt";
try
{
// Check if file exists or not
if (File.Exists(path))
{
File.Delete(path);
}
// Write data into file using StreamWriter through the path
using (StreamWriter sw = new StreamWriter(myfilepath))
{
sw.WriteLine("hello");
sw.WriteLine("geeks for geeks");
sw.WriteLine("welcome to c#");
}
// Read the file present in the path
using (StreamReader sr = new StreamReader(myfilepath))
{
// Iterating the file
while (sr.Peek() >= 0)
{
// Read the data in the file until the peak
Console.WriteLine(sr.ReadLine());
}
}
}
// Caught the exception
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
Console.Read();
}
Output:
hello
geeks for geeks
welcome to c#
See the file data is inserted:
Similar Reads
Reading Lines by Lines From a File to a Vector in C++ STL Prerequisites: STL in C++Vector in C++File handling in C++ The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms, and iterators. Vector in C
2 min read
C++ Program to Read Content From One File and Write it Into Another File Here, we will see how to read contents from one file and write it to another file using a C++ program. Let us consider two files file1.txt and file2.txt. We are going to read the content of file.txt and write it in file2.txt Contents of file1.txt: Welcome to GeeksForGeeks Approach: Create an input f
2 min read
How to Read a File Line by Line to String in Golang? To read a file line by line the bufio package Scanner is used. Let the text file be named as sample.txt and the content inside the file is as follows: GO Language is a statically compiled programming language, It is an open-source language. It was designed at Google by Rob Pike, Ken Thompson, and Ro
2 min read
What is the Efficient Way of Reading a Huge Text File? In C++, reading a large text file efficiently requires a careful approach to ensure optimal performance in terms of memory usage and processing speed. In this article, we will learn how to read a huge text file efficiently in C++. Read a Large Text File Efficiently in C++The most efficient way to re
2 min read
How to Read File into String in C++? In C++, file handling allows us to read and write data to an external file from our program. In this article, we will learn how to read a file into a string in C++. Reading Whole File into a C++ StringTo read an entire file line by line and save it into std::string, we can use std::ifstream class to
2 min read