C# Program to Read Data from Stream and Convert Data to Chars
Posted by Superadmin on August 13 2022 10:10:32

C# Program to Read Data from Stream and Convert Data to Chars

 

 

This is a C# Program to read data from stream and cast data to chars.

Problem Description

This C# Program Reads Data from Stream and Cast Data to Chars.

Problem Solution

Here the string from the file is converted into character data.

Program/Source Code

Here is source code of the C# Program to Read Data from Stream and Cast Data to Chars. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Read Data from Stream and Cast Data to Chars
 */
using System;
using System.IO;
public sealed class Program
{
    public static void Main()
    {
        using (Stream s = new FileStream(@"c:\sri\srip.txt", FileMode.Open))
        {
            int read;
            while ((read = s.ReadByte()) != -1)
            {
                Console.Write("{0} ", (char)read);
            }
            Console.ReadLine();
        }
    }
}
Program Explanation

This C# program is used to read data from stream and cast data to chars. The ReadByte() function is used to read a byte from the file and advances read the position one byte. Using while loop checks the line is not equal to -1. If the condition is true then execute the iteration of the loop. The string from the file is converted into character data.

 
Runtime Test Cases
 
G O O D M O R N I N G