For example, we create a EideStream obecteex reading a file naned sample.txt as
ID: 3590018 • 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
//Program1.cs
/**
The c sharp program that open a text file called
* sample.txt and read text file data into string data
* and display to console
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ReadingTextFile
{
class Program
{
static void Main(string[] args)
{
//set filename
string fileName = "sample.txt";
string data;
//create an instance of FileStream
//in open mode and read mode
FileStream fs =
new FileStream(fileName,FileMode.Open ,FileAccess.Read);
//read text file data usign StringStream
using (StreamReader sr = new StreamReader(fs))
{
data = sr.ReadToEnd();
}
//print to console
Console.WriteLine(data);
Console.ReadLine();
}
}
}
sample.txt file :
A groupbox controls allows user to select one radio button at a time.
Any time, among any number of radio buttons, only one is allowed to select.
Sample output screen:
----------------------------------------------------------------------
2)
//Program2.cs
/**
The c sharp program that create a text file called
* integers1to50.txt and write 1 to 50 numbers
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ReadingTextFile
{
class Program
{
static void Main(string[] args)
{
//set filename
string fileName = "integers1to50.txt";
//create an instance of FileStream
//in createNew to write data
FileStream fs =
new FileStream(fileName,FileMode.CreateNew ,FileAccess.Write);
//craete an instance of StreamWriter class with fs
StreamWriter sw = new StreamWriter(fs);
//read 1 to 50 to file
for (int i = 1; i <= 50; i++)
{
sw.Write(i+" ");
}
sw.Close();
//close fs
fs.Close();
Console.WriteLine("File is successfully written");
//print to console
Console.ReadLine();
}
}
}
Sample Output :
integers1to50.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
-------------------------------------------------------------------------------------
//Program3.cs
/**
The c sharp program that opens an existing a text file called
* integers1to50.txt and write name to text file
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ReadingTextFile
{
class Program
{
static void Main(string[] args)
{
//set filename
string fileName = "integers1to50.txt";
//create an instance of FileStream
//in createNew to write data
FileStream fs =
new FileStream(fileName,FileMode.Append ,FileAccess.Write);
//craete an instance of StreamWriter class with fs
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine();
//Append name to end of file
sw.WriteLine("-----------------");
sw.WriteLine("NAME: XXXX");
sw.WriteLine("-----------------");
sw.Close();
//close fs
fs.Close();
Console.WriteLine("File is successfully written");
//print to console
Console.ReadLine();
}
}
}
Sample Output:
integers1to50.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
-----------------
NAME: XXXX
-----------------
Note: Look for output text or input text files in debug folder of the project
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.