2012-02-21 8 views
0

私は選択したファイルパスを返すウィンドウ用のファイルチューザを作成しました。私は与えられたファイルを読んでみたいですが、ファイルパスを正しい関数に渡す方法はわかりません。ファイルパス(テキストボックス値)をC++のファイル読み込み関数に渡す方法は?

ファイルForm1.h私はボタンアクションを持っており、その中に私がopenFileDialog1->FileNameを得ることができますが、私はmain.cppファイルの内部でreadFile()関数に、この変数を渡す方法がわかりません。

私はパスを返すようにメソッドを作成しました:

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; 
     } 

ここ

System::String^ filePath; 
....  
    private: System::String^ getPath() { return filePath; } 

は、ファイル・ピッカーコードです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; 
} 

答えて

1

その後、(それはあなたが好むものだならば、パブリックフィールド)をForm1クラスにパブリックプロパティにファイル名を入れて(またはあなたのある、getPath()メソッドを公開する):

Form1^ form = gcnew Form1(); 
Application::Run(form); 
String^ p1 = form->FileName; 
関連する問題