私はcsvファイルから値を読み取り、それを特定の場所の.c
ファイルに更新するコンソールアプリケーションを開発しました。私はそれを拡張し、より良いGUIのためのexeファイルを作成する必要があります。しかし、コンソールアプリケーションで動作する同じコードは、Visual Studio 2010のWindowsフォームにエラーを投げています。 getline
、eof()
、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;
}
理由標準ライブラリのネイティブC++コードと.NetフレームワークのマネージC++ CLIコードを混在させることです。 1つまたは別のものを使用すると、動作します。 –
あなたは詳細を教えてください...私はいくつかのライブラリを追加するか、マイナーな変更を行うことによって、Windowsフォームで動作するネイティブC++コードを作成できますか? –
ここではコンパイラのエラーを質問に表示して、人々が簡単に役立つようにしてください。 –