C# Program to Get Content from a File and Read the Content 1 Byte at a Time
Posted by Superadmin on August 13 2022 10:09:11

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.

 

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