0% found this document useful (0 votes)
13 views

StreamExtend Cs

The document shows how to write different data types like integers, booleans, and strings to a file and then read them back from the file. It creates a MyWriter object to write the data and a MyReader object to read the data back from the test file.

Uploaded by

Slime UNICORN
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

StreamExtend Cs

The document shows how to write different data types like integers, booleans, and strings to a file and then read them back from the file. It creates a MyWriter object to write the data and a MyReader object to read the data back from the test file.

Uploaded by

Slime UNICORN
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

using System.IO;

class Extend
{
public static void Main( string[] args )
{
// create a file for testing
FileStream s = File.Create( "test.txt" );
s.Close();

// create MyWriter object


MyWriter write = new MyWriter( "test.txt" );

// write some integers to a file


Console.WriteLine(
"Writing the following integers: 4, 38764, 20948" );
write.WriteInteger( 4 );
write.WriteInteger( 38764 );
write.WriteInteger( 20948 );

// write some booleans


Console.WriteLine(
"Writing the following booleans: true, false" );
write.WriteBoolean( true );
write.WriteBoolean( false );

string day = "Have a nice day";


string year = "Happy new year!";

// write some strings


Console.WriteLine( "Writing the following strings: \""
+ day + "\", \"" + year + "\"" );
write.WriteString( day );
write.WriteString( year );

write.Close(); // close the file

MyReader read = new MyReader( "test.txt" );

// read integers back from the file


int firstInt = read.ReadInteger();
int secondInt = read.ReadInteger();
int thirdInt = read.ReadInteger();

Console.WriteLine( "\nRead the following integers: " +


"{0}, {1}, {2}", firstInt, secondInt, thirdInt );

// read booleans
bool firstBool = read.ReadBoolean();
bool secondBool = read.ReadBoolean();

Console.WriteLine( "Read the following booleans: " +


"{0}, {1}", firstBool, secondBool );

// read strings
string firstString = read.ReadString();
string secondString = read.ReadString();
Console.WriteLine( "Read the following strings: " +
"\"{0}\", \"{1}\"", firstString, secondString );
} // end Main
} // end class Extend

You might also like