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.

advertisement
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

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.

advertisement
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn