string型からの変換 .c_str() について
Posted: 2015年3月13日(金) 12:10
どうにもわからなかったので質問させてください
まずやりたいことですが、ビットマップファイルの中身を一定の法則に従って変換したいと思っています。
バイナリエディタを使い1つ1つやることで可能ですが、非現実的ですので、そういうアプリケーションを作るのが目的です。
ビットマップファイルの数もそれなりにあるので、ファイルのドラッグ&ドロップで一気に出来るようにしたいと考えています。
環境はVisual Basic 2010 Express c++ です
http://d.hatena.ne.jp/n2n/20060829/p1
ここを参考にほぼそのままコードを書いて試したところ
ドラッグ&ドロップした複数ファイルのパスを、順に表示することは出来ました。
しかし、ファイルのパスが入っている変数が string型 というものらしく
fopen等で開くためにはchar型への変換が必要とあったので、.c_strを使って変換しようとしたのですが
これが全く上手くいきません。
「’.c_str()’の左側はクラス、構造体、共用体でなければなりません。」というエラーが出ます。
正直なところ、ほぼ丸写ししたためにコードの意味もろくに理解していないのですが、どこをどう直せばいいのでしょうか?
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string>
namespace 変換 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 の概要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ここにコンストラクター コードを追加します
//
}
protected:
/// <summary>
/// 使用中のリソースをすべてクリーンアップします。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// 必要なデザイナー変数です。
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// デザイナー サポートに必要なメソッドです。このメソッドの内容を
/// コード エディターで変更しないでください。
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// Form1
//
this->AllowDrop = true;
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 273);
this->Name = L"Form1";
this->Text = L"Form1";
this->DragDrop += gcnew System::Windows::Forms::DragEventHandler(this, &Form1::Form1_DragDrop);
this->DragEnter += gcnew System::Windows::Forms::DragEventHandler(this, &Form1::Form1_DragEnter);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_DragEnter(System::Object^ sender, System::Windows::Forms::DragEventArgs^ e) {
//ドラッグされたのがファイルなら受け付ける、それ以外なら無視
if(e->Data->GetDataPresent(::System::Windows::Forms::DataFormats::FileDrop)){
e->Effect=::System::Windows::Forms::DragDropEffects::All;
}
else{
e->Effect=::System::Windows::Forms::DragDropEffects::None;
}
}
private: System::Void Form1_DragDrop(System::Object^ sender, System::Windows::Forms::DragEventArgs^ e) {
//ドロップされたときの処理
if(e->Data->GetDataPresent(::System::Windows::Forms::DataFormats::FileDrop)){
array<String^>^files = (array<String^>^)e->Data->GetData(DataFormats::FileDrop);
FILE *fp;
char *s;
for(int i=0;i<files->Length;i++){
MessageBox::Show( files );
*s=files.c_str();//ここでエラー
/*if ((fp=fopen(s,"rb"))==NULL){
MessageBox::Show("なんかエラーです");
exit(EXIT_FAILURE);
}
fclose(fp);*/
}
}
}
};
}
まずやりたいことですが、ビットマップファイルの中身を一定の法則に従って変換したいと思っています。
バイナリエディタを使い1つ1つやることで可能ですが、非現実的ですので、そういうアプリケーションを作るのが目的です。
ビットマップファイルの数もそれなりにあるので、ファイルのドラッグ&ドロップで一気に出来るようにしたいと考えています。
環境はVisual Basic 2010 Express c++ です
http://d.hatena.ne.jp/n2n/20060829/p1
ここを参考にほぼそのままコードを書いて試したところ
ドラッグ&ドロップした複数ファイルのパスを、順に表示することは出来ました。
しかし、ファイルのパスが入っている変数が string型 というものらしく
fopen等で開くためにはchar型への変換が必要とあったので、.c_strを使って変換しようとしたのですが
これが全く上手くいきません。
「’.c_str()’の左側はクラス、構造体、共用体でなければなりません。」というエラーが出ます。
正直なところ、ほぼ丸写ししたためにコードの意味もろくに理解していないのですが、どこをどう直せばいいのでしょうか?
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string>
namespace 変換 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 の概要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ここにコンストラクター コードを追加します
//
}
protected:
/// <summary>
/// 使用中のリソースをすべてクリーンアップします。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// 必要なデザイナー変数です。
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// デザイナー サポートに必要なメソッドです。このメソッドの内容を
/// コード エディターで変更しないでください。
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// Form1
//
this->AllowDrop = true;
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 273);
this->Name = L"Form1";
this->Text = L"Form1";
this->DragDrop += gcnew System::Windows::Forms::DragEventHandler(this, &Form1::Form1_DragDrop);
this->DragEnter += gcnew System::Windows::Forms::DragEventHandler(this, &Form1::Form1_DragEnter);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_DragEnter(System::Object^ sender, System::Windows::Forms::DragEventArgs^ e) {
//ドラッグされたのがファイルなら受け付ける、それ以外なら無視
if(e->Data->GetDataPresent(::System::Windows::Forms::DataFormats::FileDrop)){
e->Effect=::System::Windows::Forms::DragDropEffects::All;
}
else{
e->Effect=::System::Windows::Forms::DragDropEffects::None;
}
}
private: System::Void Form1_DragDrop(System::Object^ sender, System::Windows::Forms::DragEventArgs^ e) {
//ドロップされたときの処理
if(e->Data->GetDataPresent(::System::Windows::Forms::DataFormats::FileDrop)){
array<String^>^files = (array<String^>^)e->Data->GetData(DataFormats::FileDrop);
FILE *fp;
char *s;
for(int i=0;i<files->Length;i++){
MessageBox::Show( files );
*s=files.c_str();//ここでエラー
/*if ((fp=fopen(s,"rb"))==NULL){
MessageBox::Show("なんかエラーです");
exit(EXIT_FAILURE);
}
fclose(fp);*/
}
}
}
};
}