C# Program to Get Content from a File and Read the Content 1 Byte at a Time

This is a C# Program to get content from a file and read the content 1 byte at a time.

Problem Description

This C# Program Gets Content from a File and Read the Content 1 Byte at a Time.

Problem Solution

Here it firsts gets the contents from a file and reads the content per byte.

Program/Source Code

Here is source code of the C# Program to Get Content from a File and Read the Content 1 Byte at a Time. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Get Content from a File and Read the Content 1 Byte at a Time
 */
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} ", read);
            }
            Console.ReadLine();
        }
    }
}
Program Explanation

This C# program is used to get content from a file and read the content 1 byte at a time. Using ReadByte() function, we are reading a byte from the file and advances the read position one byte.

advertisement
advertisement

While loop is used to check the line is not equal to -1. If the condition is true then execute the iteration of the loop. Here it firsts gets the contents from a file and reads the content per byte and print the value.

Runtime Test Cases
 
71 79 79 68 77 79 82 78 73 78 71

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.