Here is the data file:
4
2 1 -1 2 -6
3 4 0 1 1
1 5 2 6 -3
5 2 -1 -1 3
Here is the correct code below to read numbers separated by each space from a text file above into a two dimensional array, the results of the two dimensional array is displayed in a text box :
Code:
private: System::Void buttonTestSplitFunction_Click(System::Object^ sender, System::EventArgs^ e) {
Code:
int theRow;
int theCol;
String^ errorResults = " ";
String^ results = "";
int maxRow = 10;
int maxColumn = 11;
array<double,2>^ Equation = gcnew array<double,2>(maxRow,maxColumn);
array<String^>^splitNumber;
if (System::Windows::Forms::DialogResult::OK != openFileDialog1->ShowDialog(this))
return;
System::IO::StreamReader^ sr = gcnew System::IO::StreamReader(openFileDialog1->FileName);
try
{
int n = int::Parse(sr->ReadLine());
theRow = 0;
while (!(sr->EndOfStream))
{
String^ numbers = sr->ReadLine();
array<wchar_t>^charsSpace = {' '};
splitNumber = numbers->Split(charsSpace);
for (theCol = 0; theCol <= n; theCol++)
Equation[theRow,theCol] = double::Parse(splitNumber[theCol]);
theRow++;
}
results = results + "From your data file : " + "\"" + this->openFileDialog1->FileName + "\"" + Environment::NewLine + Environment::NewLine;
results = results + n.ToString() + Environment::NewLine;
for(int i = 0 ; i <= n-1; i++)
{
for(int j = 0; j <= n; j++)
{
results = results + (Equation[i,j]).ToString();
}
results = results + Environment::NewLine;
}
this->textBoxSplitResults->Text = results;
}
catch(FormatException^ message)
{
errorResults = errorResults + "Your file : " + "\"" + this->openFileDialog1->FileName->ToString()+ "\"" + " cause a format exception because it contains incorrect data format to run this program!!" + Environment::NewLine + Environment::NewLine;
errorResults = errorResults + message->ToString();
this->textBoxErrorResults->Text = errorResults;
}
catch(OverflowException^ message)
{
errorResults = errorResults + "Your file : " + "\"" + this->openFileDialog1->FileName->ToString()+ "\"" + " cause an overflow exception because the data values either too big or too small for this program to handle!!" + Environment::NewLine + Environment::NewLine;
errorResults = errorResults + message->ToString();
this->textBoxErrorResults->Text = errorResults;
}
finally
{
sr->Close();
}
}