What’s wrong with this code? Find the error(s) in the following code, which is supposed to read a line from some- file.txt, convert the line to uppercase and then append it to somefile.txt.


string strPath = "somefile.txt";
string strContents;

StreamWriter objStreamWriter;
objStreamWriter = new StreamWriter( strPath, true );

StreamReader objStreamReader;
objStreamReader = new StreamReader( strPath );

strContents = objStreamReader.ReadLine();

strContents = strContents.ToUpper();

objStreamWriter.Write( strContents );

objStreamReader.Close();
objStreamWriter.Close();


Once the StreamWriter opens the file specified by strPath, the file is marked

open. While open, no other object can open the file. Thus a run-time error occurs when StreamReader tries to open the same file. The complete incorrect code reads:





// MakeUppercase.cs (Incorrect)



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.IO;



namespace MakeUppercase

{

///

/// Summary description for FrmMakeUppercase.

///


public class FrmMakeUppercase : System.Windows.Forms.Form

{

private System.Windows.Forms.Label lblResult;

private System.Windows.Forms.TextBox txtDisplay;

///

/// Required designer variable.

///


private System.ComponentModel.Container components = null;



public FrmMakeUppercase()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();



//

// TODO: Add any constructor code after InitializeComponent

// call

//

}



///

/// Clean up any resources being used.

///


protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}



// Windows Form Designer generated code



///

/// The main entry point for the application.

///


[STAThread]

static void Main()

{

Application.Run( new FrmMakeUppercase() );

}



// converts a line from somefile.txt to uppercase and

// appends it to the end of the file

private void FrmMakeUppercase_Load(

object sender, System.EventArgs e )

{

string strPath = "somefile.txt";

string strContents;



StreamWriter objStreamWriter;

objStreamWriter = new StreamWriter( strPath, true );

StreamReader objStreamReader;

objStreamReader = new StreamReader( strPath );



strContents = objStreamReader.ReadLine();



strContents = strContents.ToUpper();



objStreamWriter.Write( strContents );



objStreamReader.Close();

objStreamWriter.Close();



// display the results

objStreamReader = new StreamReader( strPath );



strContents = objStreamReader.ReadLine();

txtDisplay.Text += strContents + "\t”;



strContents = strContents.ToUpper();

txtDisplay.Text += strContents;



objStreamReader.Close();



} // end method FrmMakeUppercase_Load



} // end class FrmMakeUppercase

}







The complete corrected code should read:



// MakeUppercase.cs (Correct)



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.IO;



namespace MakeUppercase

{

///

/// Summary description for FrmMakeUppercase.

///


public class FrmMakeUppercase : System.Windows.Forms.Form

{

private System.Windows.Forms.Label lblResult;

private System.Windows.Forms.TextBox txtDisplay;

///

/// Required designer variable.

///


private System.ComponentModel.Container components = null;



public FrmMakeUppercase()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();



//

// TODO: Add any constructor code after InitializeComponent

// call

//

}



///

/// Clean up any resources being used.

///


protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}



// Windows Form Designer generated code



///

/// The main entry point for the application.

///


[STAThread]

static void Main()

{

Application.Run( new FrmMakeUppercase() );

}



// converts a line from somefile.txt to uppercase and

// appends it to the end of the file

private void FrmMakeUppercase_Load(

object sender, System.EventArgs e )

{

string strPath = "somefile.txt";

string strContents;



StreamWriter objStreamWriter;

StreamReader objStreamReader;

objStreamReader = new StreamReader( strPath );

strContents = objStreamReader.ReadLine();

strContents = strContents.ToUpper();

objStreamReader.Close();

objStreamWriter = new StreamWriter( strPath, true );

objStreamWriter.Write( strContents );

objStreamWriter.Close();



// display the results

objStreamReader = new StreamReader( strPath );



strContents = objStreamReader.ReadLine();

txtDisplay.Text += strContents + "\t”;



strContents = strContents.ToUpper();

txtDisplay.Text += strContents;



objStreamReader.Close();



} // end method FrmMakeUppercase_Load



} // end class FrmMakeUppercase

}



Computer Science & Information Technology

You might also like to view...

__________ topology is usually used when a network layout already exists; however, it needs to be expanded using a different type of layout.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

A narrow column used to display links and content of secondary importance is called a(n) nav.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

____ was developed by the Massachusetts Institute of Technology (MIT) and used to verify the identity of network users.

A. Kerberos B. Dynamic WEP C. WEP2 D. LDAP

Computer Science & Information Technology

The organization you work for allows users to work from their own mobile devices. What is this referred to as?

a. Mobile hotspot b. IoT c. BYOD d. IR

Computer Science & Information Technology