How to read an array of string from a text file

Hi there,

How can I write a program in Windows Forms Application using VS, C++, that reads an array of strings (mostly numeric) from a text file? What I want to do is to read the strings and then put them on the texboxes and comboboxes in the Windows Form.

The example of the text file looks like (space separated)
1 10 17 9 50
5 8 9
8 7

I googled around and tried the following code, I tried to read the first column in the second row = 5, however the following code puts the entire row "5 8 9" into the textbox.




Thanks for your help


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  private: System::Void openToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e)
{

			 OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
			 Stream^ myStream;
			 if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
			 {
				 if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
				 {
					 String^ FilePath = openFileDialog1->FileName;
					 //System::IO::StreamReader^ sr = gcnew  System::IO::StreamReader(openFileDialog1->FileName);
					 array <String^>^ readLines = File::ReadAllLines(FilePath);
		                         // comboBox1->SelectedIndex = (int)System::Convert::ToDouble(readLines[0][0]);
					 textBox7->Text = readLines[1];
					 //MessageBox::Show(sr->ReadToEnd());
					 myStream->Close();

				  }
			 }



 }
Last edited on
textBox7->Text = readLines[1];
readLines[1] is the whole line.

If you need only the first number you can use the Split function of the string class.
https://msdn.microsoft.com/en-us/library/e3awe53k.aspx
Thanks Thomas, it worked!
Topic archived. No new replies allowed.