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.

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

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

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.