2016-04-14 6 views
-2

私はcsvファイルから値を読み取り、それを特定の場所の.cファイルに更新するコンソールアプリケーションを開発しました。私はそれを拡張し、より良いGUIのためのexeファイルを作成する必要があります。しかし、コンソールアプリケーションで動作する同じコードは、Visual Studio 2010のWindowsフォームにエラーを投げています。 getlineeof()file.openなどのコマンドはエラーを投げています。私はコーディングで初心者であり、どんな種類の助けでも感謝します。Windowsフォームでのexeファイルの生成(C++を使用)

 #pragma once 
     #include <iostream> 
     #include <fstream> 
     #include <string> 
     #include "stdafx.h" 
     namespace Application1 

    { 

    using namespace System; 
    using namespace System::ComponentModel; 
    using namespace System::Collections; 
    using namespace System::Windows::Forms; 
    using namespace System::Data; 
    using namespace System::Drawing; 
    using namespace System::IO; 


    /// <summary> 
    /// Summary for Form1 
    /// </summary> 
    public ref class Form1 : public System::Windows::Forms::Form 
    { 
    public: 
     Form1(void) 
     { 
      InitializeComponent(); 
      // 
      //TODO: Add the constructor code here 
      // 
     } 

    protected: 
     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     ~Form1() 
     { 
      if (components) 
      { 
       delete components; 
      } 
     } 
    private: System::Windows::Forms::Button^ button1; 
    private: System::Windows::Forms::OpenFileDialog^ openFileDialog1; 

    private: System::Windows::Forms::Button^ button2; 
    private: System::Windows::Forms::Button^ button3; 

    protected: 

    private: 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     System::ComponentModel::Container ^components; 

#pragma region Windows Form Designer generated code 
     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     void InitializeComponent(void) 
     { 
      this->button1 = (gcnew System::Windows::Forms::Button()); 
      this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog()); 
      this->button2 = (gcnew System::Windows::Forms::Button()); 
      this->button3 = (gcnew System::Windows::Forms::Button()); 
      this->SuspendLayout(); 
      // 
      // button1 
      // 
      this->button1->Location = System::Drawing::Point(197, 82); 
      this->button1->Name = L"button1"; 
      this->button1->Size = System::Drawing::Size(75, 42); 
      this->button1->TabIndex = 0; 
      this->button1->Text = L"browse excel file"; 
      this->button1->UseVisualStyleBackColor = true; 
      this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); 
      // 
      // openFileDialog1 
      // 
      this->openFileDialog1->FileName = L"openFileDialog1"; 
      // 
      // button2 
      // 
      this->button2->Location = System::Drawing::Point(197, 193); 
      this->button2->Name = L"button2"; 
      this->button2->Size = System::Drawing::Size(75, 23); 
      this->button2->TabIndex = 2; 
      this->button2->Text = L"Run"; 
      this->button2->UseVisualStyleBackColor = true; 
      this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click); 
      // 
      // button3 
      // 
      this->button3->Location = System::Drawing::Point(197, 150); 
      this->button3->Name = L"button3"; 
      this->button3->Size = System::Drawing::Size(75, 23); 
      this->button3->TabIndex = 3; 
      this->button3->Text = L"browse c file"; 
      this->button3->UseVisualStyleBackColor = true; 
      this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click); 
      // 
      // Form1 
      // 
      this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
      this->ClientSize = System::Drawing::Size(284, 262); 
      this->Controls->Add(this->button3); 
      this->Controls->Add(this->button2); 
      this->Controls->Add(this->button1); 
      this->Name = L"Form1"; 
      this->Text = L"Form1"; 
      this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); 
      this->ResumeLayout(false); 

     } 
#pragma endregion 
    private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { 
      } 
    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { 
       openFileDialog1->Filter = "Csv Files|*.csv"; 
       openFileDialog1->Title = "Select a Csv File"; 
        if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) 
     { 

     System::IO::StreamReader^sr = gcnew 
      System::IO::StreamReader(openFileDialog1->FileName); 
     MessageBox::Show(sr->ReadToEnd()); 
     sr->Close(); 
     } 
      } 
       private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) { 
          openFileDialog1->Filter = "C Files|*.c"; 
       openFileDialog1->Title = "Select a C File"; 
        if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) 
     { 

     System::IO::StreamReader^sr = gcnew 
      System::IO::StreamReader(openFileDialog1->FileName); 
     sr->Close(); 
     } 
         } 
    private: System::Void listBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) { 
      } 
    private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { 

String^s1,^s2,^s3; 
String^filename,^text,^line; 
String^cfilename,^ funcname,^signal,^value; 
String^line1,^line2; 

StreamReader^file=gcnew StreamReader(filename); 
{ 
    while(getline(file, cfilename, ',') && getline(file, funcname, ',') 
     && getline(file, signal, ',') && getline(file, value, '\n')) 
     { 

       s1=signal; s2=value;    

       s2 = s2 + "; //"; 

       int offset, inset; 

       String^line; 

       StreamWriter^cfile=gcnew StreamWriter(cfilename); 

       //fstream^cfile(cfilename); 

       StreamWriter^fileOutput=gcnew StreamWriter("output.c"); 

       //fstream^fileOutput; 

       fileOutput->open("output.c"); 

       if(cfile->OpenFile() && fileOutput->OpenFile()) 
       { 
         while(!cfile->eof()) 
          {          
           if (getline(cfile, line))        
           { 

           if((offset = line->find(funcname, 0)) != string::npos) 
             { 

             MessageBox::Show("found: " << funcname);  

             fileOutput->WriteLine(line); 

            while(getline(cfile,line1)) 
              { 

              if((inset=line1.find(s1, 0)) !=string::npos) 
               { 

               MessageBox::Show("found: " << s1); 

               string^s3 = s1+ "=" +s2; 

               MessageBox::Show(s3); 

               line1->replace(inset, inset+s1.size(), s3); 

               }  
              fileOutput->Writeline(line1); 
              }         
             } 

           fileOutput->Writeline(line); 

            } 
           } 
          }      
         cfile->close(); 

         fileOutput->close(); 

         cfile->open(cfilename); 

         fileOutput->open("output.c"); 

         if(cfile.is_open() && fileOutput.is_open()) 
          while(getline(fileOutput,line2)) 
           cfile->Writeline(line2); 
         cfile->close(); 
         fileOutput->close(); 
         } 

        } 
     file->close(); 
     remove("output.c"); 
     getchar(); 
     return 0; 
    } 

} 
;} 
} 

私の働いC++コンソールアプリケーションのコードは以下の通りである:あなたが使用しているもの

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

int main() 
{ 
string s1, s2, s3; 
string filename, text, line; 
string cfilename,funcname, signal, value; 
string line1,line2; 


cout << "Enter excel filename" << endl; 
cin >> filename; 

fstream file(filename); 

if (!file) { 
      cerr << "No such file exists or the file is open." << endl; 
      exit(1); 
      } 
if(file.is_open()) 
{ 
    while(getline(file, cfilename, ',') && getline(file, funcname, ',') 
     && getline(file, signal, ',') && getline(file, value, '\n')) 
     { 
     cout << cfilename << '\t' << funcname << '\t' << signal << '\t' << value << '\n'; 

     string s1=signal,s2=value;   

       s2 = s2 + "; //"; 

       int offset, inset; 

       string line; 

       fstream cfile(cfilename); 

       fstream fileOutput; 

       fileOutput.open("output.c"); 

       if(cfile.is_open() && fileOutput.is_open()) 
       { 
         while(!cfile.eof()) 
          {          
           if (getline(cfile, line)) 
           { 

           if((offset = line.find(funcname, 0)) != string::npos) 
             { 

             cout << "found: " << funcname << endl; 

             fileOutput << line << '\n'; 

            while(getline(cfile,line1)) 
              { 

              if((inset=line1.find(s1, 0)) !=string::npos) 
               { 
               cout<<"found: " << s1 << endl; 

               string s3 = s1+ "=" +s2; 

               cout<<s3<<'\n'; 

               line1.replace(inset, inset+s1.size(), s3); 

               }  
              fileOutput << line1 << '\n'; 
              }         
             } 

           fileOutput << line << '\n'; 

            } 
           } 
          }      
         cfile.close(); 

         fileOutput.close(); 

         cfile.open(cfilename); 

         fileOutput.open("output.c"); 

         if(cfile.is_open() && fileOutput.is_open()) 
          while(getline(fileOutput,line2)) 
           cfile<<line2<<'\n'; 
         cfile.close(); 
         fileOutput.close(); 
         } 

        } 
     file.close(); 
     getchar(); 
     return 0; 
    } 

These are the errors i am facing.

+1

理由標準ライブラリのネイティブC++コードと.NetフレームワークのマネージC++ CLIコードを混在させることです。 1つまたは別のものを使用すると、動作します。 –

+0

あなたは詳細を教えてください...私はいくつかのライブラリを追加するか、マイナーな変更を行うことによって、Windowsフォームで動作するネイティブC++コードを作成できますか? –

+0

ここではコンパイラのエラーを質問に表示して、人々が簡単に役立つようにしてください。 –

答えて

2

C++ CLIプロジェクトは、ネイティブC++コードをコンパイルすることができ、また、C++コードで管理します。たとえば、電話をかけようとしただけでコードがコンパイルされません。 std::getline(System::String^, ...)、管理されたC++パラメータを持つネイティブC++関数を呼び出すものstd::getlineを使用することはできますが、代わりにstd::stringを使用する必要があります。私はあなたのコードを変更したので、コンパイルすることができます。私はStreamReaderを削除しました。なぜなら、ファイルを閉じるために開くことを混乱させる必要がないからです。

Form1.h

#pragma once 

namespace Application1 
{ 
    public ref class Form1 : public System::Windows::Forms::Form 
    { 
    public: 
     Form1(); 

    protected: 
     ~Form1(); 

    private: System::Void Run(System::String^ FileName, System::String^ OutputFileName); 
    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e); 
    private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e); 
    private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e); 

    private: System::Windows::Forms::Button^ button1; 
    private: System::Windows::Forms::OpenFileDialog^ openFileDialog1; 
    private: System::Windows::Forms::Button^ button2; 
    private: System::Windows::Forms::Button^ button3; 
    private: System::ComponentModel::Container^ components; 

    #pragma region Windows Form Designer generated code 
    void InitializeComponent(void) 
    { 
     this->button1 = (gcnew System::Windows::Forms::Button()); 
     this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog()); 
     this->button2 = (gcnew System::Windows::Forms::Button()); 
     this->button3 = (gcnew System::Windows::Forms::Button()); 
     this->SuspendLayout(); 
     // 
     // button1 
     // 
     this->button1->Location = System::Drawing::Point(197, 82); 
     this->button1->Name = L"button1"; 
     this->button1->Size = System::Drawing::Size(75, 42); 
     this->button1->TabIndex = 0; 
     this->button1->Text = L"browse excel file"; 
     this->button1->UseVisualStyleBackColor = true; 
     this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); 
     // 
     // openFileDialog1 
     // 
     this->openFileDialog1->FileName = L"openFileDialog1"; 
     // 
     // button2 
     // 
     this->button2->Location = System::Drawing::Point(197, 193); 
     this->button2->Name = L"button2"; 
     this->button2->Size = System::Drawing::Size(75, 23); 
     this->button2->TabIndex = 2; 
     this->button2->Text = L"Run"; 
     this->button2->UseVisualStyleBackColor = true; 
     this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click); 
     // 
     // button3 
     // 
     this->button3->Location = System::Drawing::Point(197, 150); 
     this->button3->Name = L"button3"; 
     this->button3->Size = System::Drawing::Size(75, 23); 
     this->button3->TabIndex = 3; 
     this->button3->Text = L"browse c file"; 
     this->button3->UseVisualStyleBackColor = true; 
     this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click); 
     // 
     // Form1 
     // 
     this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
     this->ClientSize = System::Drawing::Size(284, 262); 
     this->Controls->Add(this->button3); 
     this->Controls->Add(this->button2); 
     this->Controls->Add(this->button1); 
     this->Name = L"Form1"; 
     this->Text = L"Form1"; 
     this->ResumeLayout(false); 
    } 
#pragma endregion 
    }; 
} 

Form1.cpp

#include "stdafx.h" 
#include "Form1.h"   

namespace Application1 
{ 
    Form1::Form1() 
    { 
     InitializeComponent(); 
    } 


    Form1::~Form1() 
    { 
     if (components) 
      delete components; 
    } 


    System::Void Form1::button1_Click(System::Object^ sender, System::EventArgs^ e) 
    { 
     openFileDialog1->Filter = "Csv Files|*.csv"; 
     openFileDialog1->Title = "Select a Csv File"; 

     if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) 
     {   
      System::String^ FileName = openFileDialog1->FileName; 
      System::String^ FileContent = System::IO::File::ReadAllText(FileName); 
      System::Windows::Forms::MessageBox::Show(FileContent); 
     } 
    } 


    System::Void Form1::button2_Click(System::Object^ sender, System::EventArgs^ e) 
    { 
     openFileDialog1->Filter = "Csv Files|*.csv"; 
     openFileDialog1->Title = "Select a Csv File"; 

     if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) 
     {   
      Run(openFileDialog1->FileName, "Output.txt"); 
     } 
    } 


    System::Void Form1::button3_Click(System::Object^ sender, System::EventArgs^ e) 
    { 
     openFileDialog1->Filter = "C Files|*.c"; 
     openFileDialog1->Title = "Select a C File"; 

     if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) 
     { 
      // TODO 
     } 
    } 


    System::Void Form1::Run(System::String^ FileName, System::String^ OutputFileName) 
    { 
     if (!System::IO::File::Exists(FileName)) 
     { 
      System::Windows::Forms::MessageBox::Show("File '" + FileName + "' doesn't exists"); 
      return; 
     } 

     array<System::String^>^ InputLines = System::IO::File::ReadAllLines(FileName); 
     System::Collections::Generic::List<System::String^>^ OutputLines = gcnew System::Collections::Generic::List<System::String^>(); 

     // do your procesing here 
     for (int i=0; i<InputLines->Length; i++) 
     { 
      System::String^ OutputLine = ""; 

      // sample code 
      if (InputLines[i]->Contains("abc")) 
       OutputLine += InputLines[i] + "test"; 

      OutputLines->Add(OutputLine); 
     } 

     // save output 
     System::IO::File::WriteAllLines(OutputFileName, OutputLines); 
    } 
} 

ところで、あなたは間違いなくあなたのコーディングスタイルで仕事をしなければならない:)あなたはそれをコンパイルすることができない理由

+0

入力いただきありがとうございます。 csvファイルをブラウズするための2つのボタンが追加されました。しかし、3番目のボタンを追加する際に問題がありました。これはRunで、C++コードを実行する場所です。私のC++コードのコマンドは、問題を起こしているものです。私も既存のコードで 'std :: string'を追加してみましたが、文字列はstdのメンバーではないため、エラーが発生します。 –

+0

"stringはstdのメンバーではありません"というのは、コンパイラがstd :: stringを知らないからです。#include をcppファイルに追加する必要があります。 –

+0

#include を追加しましたが、 。上記の推奨コードにC++コードをどこにどのように含めるか教えてください。私は3番目のボタンに含めることを試みたが、それは約60の奇妙なエラーに行きました。 –

関連する問題