Я создал средство выбора файлов для Windows, оно возвращает мне выбранный путь к файлу.Я хочу прочитать данный файл, но я не знаю, как передать путь к файлу нужной функции.
Файл Form1.h
У меня есть действие кнопки, и внутри него я могу получить openFileDialog1->FileName
, но яне знаю, как передать эту переменную в readFile()
функцию внутри main.cpp
файла.
Я создал метод для возврата пути:
System::String^ filePath;
....
private: System::String^ getPath() { return filePath; }
Вот файл-код сборщика:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Stream^ myStream;
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->InitialDirectory = "c:\\";
openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1->FilterIndex = 2;
openFileDialog1->RestoreDirectory = true;
if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK ){
if ( (myStream = openFileDialog1->OpenFile()) != nullptr ){
// Insert code to read the stream here.
textBox1->Text = openFileDialog1->FileName; //text box displays the chosen path
myStream->Close();
}
}
}
Переменная устанавливается при нажатии кнопки:
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
filePath = textBox1->Text;
}
Как вызвать методы возврата в моем main.cpp
:
#include "stdafx.h"
#include "Form1.h"
using namespace main;
using namespace std;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
System::String^ p1 = /*Something missing her?*/getPath1(); //I am guessing it should look like this...
return 0;
}