C++

2016-04-14 16 views
-2

kullanarak Windows formlarında exe dosyası oluşturma Csv dosyasındaki değerleri okuyan ve belirli bir konumda .c dosyasında güncelleştiren bir konsol uygulaması geliştirdim. Onu genişletmem ve daha iyi GUI için bir exe dosyası oluşturmam gerekiyor. Ancak konsol uygulaması için çalışan aynı kod görsel stüdyo 2010 pencerelerinde hatalar atıyor. getline, eof(), file.open gibi komutlar atma hatalarıdır. Kodlamada yeni başlıyorum ve her türlü yardım memnuniyetle karşılanacaktır.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; 
    } 

} 
;} 
} 

Benim uyguladığım C++ konsol uygulaması kodu aşağıdaki gibidir: Kullanmakta ne

#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

nedeni Standart kütüphaneden yerel C++ kodunu .Net çerçevesinden yönetilen C++ CLI kodu ile karıştırıyor olmanızdır. Bir ya da diğerini kullan ve işe yarayacak. –

+0

ayrıntılı bilgi verebilirsiniz ... Bazı kitaplık ekleyerek veya küçük değişiklikler yaparak Windows formlarında çalışmak için yerel C++ kodunu yapabilir miyim? –

+0

İnsanların daha kolay yardımcı olabilmesi için derleyici hatalarını burada göstermelisiniz. –

cevap

2

C++ CLI proje ++ kodu yerli c derlemek yapabiliyor ve ayrıca C++ kod başardı. Kodunuz, sadece ör. std::getline(System::String^, ...), yönetilen C++ parametresiyle native C++ işlevini çağırıyor. std::getline'u kullanmak mümkündür, ancak bunun yerine std::string'u kullanmanız gerekir. Kodunuzu değiştirdim, bu şekilde derleyebileceksiniz. StreamReader'u kaldırdım, çünkü dosyaları kapatmayı açmakla uğraşmanıza gerek kalmayacak.

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

Btw, kesinlikle kodlama stili üzerinde çalışmak zorunda :) bunu derlemek mümkün değildir neden

+0

Giriş için teşekkürler. Csv dosyalarını taramak için 2 düğme eklediniz. Ama ben C++ kodumun çalışacağı Run olan 3. düğmeyi eklemede sorun yaşadım. C++ kodumdaki komutlar sorun verenler. Ayrıca varolan koduma 'std :: string' ekleyerek denedim ancak dize bir std üyesi olmadığından bir hata veriyor. –

+0

"string std'nin bir üyesi değil" çünkü derleyici std :: string hakkında bilmediğinden ... ## cp dosyasını –

+0

içine eklemelisiniz. #include ekledim, ancak yine de kullanmam . Lütfen C++ kodumu yukarıdaki kodunuza nereye ve nasıl ekleyeceğimi söyleyin. 3. düğmeyi dahil etmeye çalıştım ama 60 tuhaf hatalara gitti. –