Visual C++ .NET File Processing 



File Processing in Microsoft .Net is primarily done using a class called FileStream. Therefore, to process a file, you can first declare a pointer to FileStream.

Automatic File Processing

Automatic file processing consists of saving and/or opening a file with little to no intervention from the user.

Imagine you create an application that contains a combo box:

For such a control, you can create a fixed list of items and only let the user select a value when necessary. That would be the case for a list of genders that includes only male and female. On the other hand, you may want your users to add or delete items from the list so that, when the application comes up, the combo box would retrieve its list of items from a list, and when the application closes, you can save the current list. To start, after the user has finished with the application and decides to close it, you can create a new file or save an existing file. Here is an example:

private: System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
	String *ListOfStates	= S"StatesList";
	FileStream *fsStates	= new FileStream(ListOfStates, FileMode::Create);
	StreamWriter *swStates = new StreamWriter(fsStates);

	for(int i = 0; i < this->cboState->Items->Count; i++)



For this exercise, the next question probably is how to fill out the combo box. You can start by creating a dialog box as follows:

Of course, you should provide a way for the user to call that dialog box:

 When the user clicks the button, you can display the States dialog box and allow the user to type a state name or abbreviation. After typing it, the user would click OK. Normally, you should provide a way to validate the user's entry. For example, you should not allow a state that is already in the list. Otherwise, this is a simple way you would add a new state to the list:

private: System::Void btnState_Click(System::Object *  sender, System::EventArgs *  e)
			 States *NState = new States;

			 if( NState->ShowDialog() == DialogResult::OK )

When the user opens the application, you can check the automatically created file and fill the combo box with items from it. Here is an example:

private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
		 String *ListOfStates	= S"StatesList";
		 FileStream *fsStates	= new FileStream(ListOfStates, FileMode::Open);
		 StreamReader *srStates = new StreamReader(fsStates);

		 String *line;

		 while( true )
			 line = srStates->ReadLine();
			 if( line == 0 )


Prompt File Processing

The most common way consists of letting the user decide when, where, and probably how to save the file. To provide this functionality, you can add one button to save and another button to open:

The buttons can be implemented as follows:

private: System::Void btnSave_Click(System::Object *  sender, System::EventArgs *  e)
	 FileStream *fs;

	 if( saveFileDialog1->ShowDialog() == DialogResult::OK )
		 fs = new FileStream(saveFileDialog1->FileName, FileMode::Create);
		 StreamWriter *sw = new StreamWriter(fs);


private: System::Void btnOpen_Click(System::Object *  sender, System::EventArgs *  e)
	 FileStream *fs;

	 if( openFileDialog1->ShowDialog() == DialogResult::OK )
		 fs = new FileStream(openFileDialog1->FileName, FileMode::Open);
		 StreamReader *sr = new StreamReader(fs);

		 txtFirstName->Text = sr->ReadLine();
		 txtLastName->Text  = sr->ReadLine();
		 txtSalary->Text    = sr->ReadLine();
		 cboState->Text     = sr->ReadLine();

private: System::Void btnReset_Click(System::Object *  sender, System::EventArgs *  e)
	 this->txtFirstName->Text = S"";
	 this->txtLastName->Text  = S"";
	 this->txtSalary->Text    = S"0.00";


Home Copyright © 2004-2012, FunctionX