0% found this document useful (0 votes)
15 views2 pages

MyReader Cs

The document defines a custom reader class that inherits from the StreamReader class and adds methods to read specific data types from a file. The class contains constructors and methods to read integers, booleans, and strings from a file.

Uploaded by

Slime UNICORN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

MyReader Cs

The document defines a custom reader class that inherits from the StreamReader class and adds methods to read specific data types from a file. The class contains constructors and methods to read integers, booleans, and strings from a file.

Uploaded by

Slime UNICORN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

using System.IO;

class MyReader : StreamReader


{
// default constructor
public MyReader( string fileName )
: base( fileName )
{
} // end constructor

// read in integer from file


public int ReadInteger()
{
// for reading an integer
char[] buffer = new char[ 10 ];
int input = 0;
string toRead = string.Empty;

// read 10 characters representing an integer from the file


Read( buffer, 0, 10 );

toRead = new string( buffer );

try
{
input = Int32.Parse( toRead );
} // end try
catch ( Exception )
{
return 0;
} // end catch

return input;
} // end method ReadInteger

// read in boolean from file


public bool ReadBoolean()
{
// for reading a boolean
char[] buffer = new char[ 5 ];
string toRead = string.Empty;

// read either 'true' or 'false'


Read( buffer, 0, 5 );

toRead = new string( buffer );

return toRead.Trim() == "true";


} // end method ReadBoolean

// read in string from file


public string ReadString()
{
// for reading a string
char[] buffer = new char[ 1 ];
string input = string.Empty;

// each string is separated by "ENDOFSTRING";


// read in one character at a time until termination
// string is reached
while ( input.LastIndexOf( "ENDOFSTRING" ) == -1 )
{
Read( buffer, 0, 1 );
input += buffer[ 0 ];
} // end while

// trim the termination string off the read string


return input.Substring( 0, input.LastIndexOf( "ENDOFSTRING" ) );
} // end method ReadString
} // end class MyReader

You might also like