Hello all,
I want to write a qui program which opens and reads a text file when pressed Open File button. So far, when I click the button Open File Dialog works and I can choose the file to read but I do not know how to read it line by line. The code for click event is below. Can you please tell me what is the next step?

#pragma endregion
	private: System::Void open_file_button_Click(System::Object^  sender, System::EventArgs^  e) {
	//we use OpenFileDialog to show a file-open dialog box
      OpenFileDialog ^ openFileDialog1 = gcnew OpenFileDialog();
      openFileDialog1->Filter = "Text Files|*.txt";
      openFileDialog1->Title = "Select the File";

      // Show the Dialog.
      
      if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
      {
      // pop up a message box if user selects a file               System::Windows::Forms::MessageBox::Show(L"deneme",L"test",System::Windows::Forms::MessageBoxButtons::OK);
      }
			 }

Recommended Answers

All 4 Replies

Thank for the tutorial. I succeeded what I want. However, I have one more question. If I want to see the 3rd line in the file what I have to do? By the way, could you please check the program and give me some feedback?
I appreciate for helps,

#pragma endregion
		
	private: System::Void open_file_button_Click(System::Object^  sender, System::EventArgs^  e)
			 {
	  String ^ file_name;
	  String ^ input;
      OpenFileDialog ^ openFileDialog1 = gcnew OpenFileDialog();
      openFileDialog1->Filter = "Text Files|*.txt";
      openFileDialog1->Title = "Select the File";

      
      if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
      {
		  file_name = System::IO::Path::GetFullPath(openFileDialog1->FileName);
		  StreamReader^ sr = File::OpenText(file_name);
		  double number_file;
		  double initial = 0;
		  double summ;
		  String ^ result;
		  while((input=sr->ReadLine())!=nullptr)
		  {
			  number_file = Convert::ToDouble(input);
			  summ = initial + number_file;
			  initial = summ;
		  }
		  result = Convert::ToString(initial);
		  System::Windows::Forms::MessageBox::Show(result,L"test",System::Windows::Forms::MessageBoxButtons::OK);
		 
	  }
			 }

>>If I want to see the 3rd line in the file what I have to do?

You have to read the file sequentially until you get to the line you want.

>>could you please check the program and give me some feedback?
It's not really necessary for anyone else to check your program, you can do it yourself just by running it and see if it works or not.

I know that anyone does not necessarily has to check my program that's why I start my question with "could you". By the way, I appreciate for your helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.