For example, we create a EideStream obecteex reading a file naned sample.txt as
ID: 3590505 • Letter: F
Question
For example, we create a EideStream obecteex reading a file naned sample.txt as shown: Parameter Description EileMode Theerustede enumerat. defines various methods for opening files. The members of the EileMode enumerator are Append: It opens an existing file and puts cursor at the end of tile, or Create: It creates a new file. creates the file, if the file does not exist. ·creatslist: It specifies to the operating system, that it should create a new file. Open: It opens an existing file. OpsnorCreass: It specifies to the operating system that it should open a tile if it exists, othervise it should create a new file. Truncate: It opens an existing file and truncates its size to zero bytes have membereadReadiristandekries have the following members : . Inheritable: It allows a file handle tpass inheritance t° the child processes None: It declines sharing of the current file Read: It allows opening the file tor reading : It allows opening the file for reading and writing Write: It allows opening the tile for writing Text file Manipulation 1- Read the text file in Ce and output it to the accesn sample.tbxt. Make sure to rename the file after downloading to your Pc 2- Create a second program to create a new text file and output the result of a loop 1 to 50 to it. 3- Open file already created file from part (1) and Append your name to it Your name Submit code and execution out out (cro-]Explanation / Answer
1.
using System;
using System.IO;
using System.Text;
public class fileread
{
static void Main()
{
string data = String.Empty;
FileStream fs = new FileStream("sample.txt", FileMode.Open, FileAccess.Read);
using (StreamReader streamReader = new StreamReader(fs, Encoding.UTF8))
{
data = streamReader.ReadToEnd();
}
Console.WriteLine(data);
}
}
2.
using System;
using System.IO;
using System.Text;
public class fileread
{
static void Main()
{
string fileName = @"sample2.txt";
using (FileStream fs = File.Create(fileName))
{
int i=0;
for(i=0;i<50;i++)
{
fs.Write(i, 0, 1);
}
}
}
}
3.
using System;
using System.IO;
class sol
{
public static void Main()
{
string file = @"sample.txt";
using (StreamWriter sw = File.AppendText(file))
{
sw.WriteLine("Samdish Arora");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.