C# Program For Reading Data From Stream and Cast Data to Chars Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given data, now our task is to read data from the stream and cast data to chars in C#. So to do this task we use the following class and methods: FileStream: It is a class that is used to read and write files. So, to manipulate files using FileStream, you need to create an object of FileStream class. Syntax: Stream object = new FileStream(path, FileMode.Open)Where path is the location of your file - @"c:\A\data.txt" and FileMode is the mode of the file like reading and writing. ReadByte(): This method is used to read the data from the file Byte by Byte. This method will throw NotSupportException when the current stream did not support reading. If the current stream is closed then this method will throw ObjectDisposedException.Syntax: FileStream_object.ReadByte()Example: Let us consider a file named "file.txt" is present in the A folder of C drive like as shown in the below image: Now we read data from stream and cast data to chars. So to this follow the following approach. Approach Read the file named "file.txt" using FileStream with the help of specified path.Read the data in file byte by byte using ReadByte() method until it reaches end of file.while ((obj = s.ReadByte()) != -1) { // Convert the data into chars and display Console.Write("{0} ", (char)obj); }Display file data into chars using Casting to char.(char)obj C# // C# program to read the data from stream // and cast data to chars using System; using System.IO; public sealed class GFG{ public static void Main() { // Read the file from the specified path using (Stream s = new FileStream(@"c:\A\file.txt", FileMode.Open)) { int obj; // Read the data in file byte by byte // Using ReadByte() method while ((obj = s.ReadByte()) != -1) { // Convert the data into chars and display Console.Write("{0} ", (char)obj); } Console.ReadLine(); } } } Output: T H I S I S F I L E D E M O Comment More infoAdvertise with us Next Article Input From Character Streams in LISP M manojkumarreddymallidi Follow Improve Article Tags : C# CSharp-programs Similar Reads Input From Character Streams in LISP This article is about input from character streams in LISP. Input in LISP can be taken in different ways like input from the user, input from character streams, etc. In LISP input from character streams can be taken from different functions accordingly. Input from character streams can be a string, 3 min read 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 Removing trailing newline character from fgets() Input fgets() reads a line from the specified stream and stores it into the string pointed by str. It stops when either (n - 1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first. However, fgets() also reads the trailing newline character and ends up r 3 min read How to read or input a string? In this article, we are going to learn how to print or output a string using different languages. Strings are considered a data type in general and are typically represented as arrays of bytes (or words) that store a sequence of characters. Strings are defined as an array of characters. Topics: How 3 min read C# | How to get the Standard Input Stream through Console Given a normal console, the task is to get the Standard Input Stream through this Console in C#. Approach: This can be done using the In property in the Console class of the System package in C#. Program: Getting the Standard Input Stream csharp // C# program to illustrate the // Console.In Property 1 min read C++ Char Data Types A Char datatype is a datatype that is used to store a single character. It is always enclosed within a single quote (' '). Syntax: Char variable; Example: C++ // C++ Program demonstrate // Use of char #include <iostream> using namespace std; int main() { char c = 'g'; cout << c; return 0 3 min read Like