【问题标题】:Generation of exe file in windows forms using c++使用 C++ 在 Windows 窗体中生成 exe 文件
【发布时间】:2016-04-14 06:56:02
【问题描述】:

我开发了一个控制台应用程序,它从 csv 文件中读取值并将其更新到特定位置的 .c 文件中。我需要扩展它并创建一个 exe 文件以获得更好的 GUI。但适用于控制台应用程序的相同代码在 Visual Studio 2010 Windows 窗体中引发错误。 getlineeof()file.open 等命令会引发错误。我是编码初学者,任何形式的帮助都将不胜感激。

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

【问题讨论】:

  • 您无法编译它的原因是,您将标准库中的本机 C++ 代码与 .Net 框架中的托管 C++ CLI 代码混合在一起。使用一个或另一个,它会起作用。
  • 能否请您详细说明... 我可以通过添加一些库或进行细微修改来使本机 C++ 代码在 Windows 窗体中工作吗?
  • 您应该在问题中显示编译器错误,以便人们可以更轻松地提供帮助。

标签: c++ visual-studio-2010 c++-cli file-handling


【解决方案1】:

您使用的 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);
    }
}

顺便说一句,你肯定需要改进你的编码风格:)

【讨论】:

  • 感谢您的意见。您添加了 2 个用于浏览 csv 文件的按钮。但是我在添加第三个按钮时遇到了问题,这是我的 c++ 代码应该运行的地方。我的 c++ 代码中的命令是有问题的。我还尝试在现有代码中添加“std::string”,但由于 string 不是 std 的成员,它会给出错误。
  • "string is not a member of std" 是因为编译器不知道 std::string ...你必须将 #include 添加到你的 cpp 文件中
  • 我已经添加了#include ,但仍然没有用。请告诉我在您上面建议的代码中的何处以及如何包含我的 c++ 代码。我尝试包含在第三个按钮中,但出现了大约 60 个奇怪的错误。
  • 你不能把它放在那里,因为在你的 c++ 例子中你试图通过 getline 从控制台读取值......但是你不能在 GUI 中使用这个函数,因为控制台不可用。您需要做的是修改您的 c++ 代码,以便可以在没有控制台的情况下启动它。
  • 你能告诉我如何使用 getline 命令读取 csv 文件中的值并将其分配给字符串吗?卡住了。
猜你喜欢
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多