• ベストアンサー

キャストの仕方(std::stringをconst char*へ)を教えてください。

c++で作成したものをコンパイルしたところ、 下記のようなエラーメッセージが表示されました。 cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int stat(const char*, stat*) 自分の解釈では、 stat関数の第1引数がconst char*なのに、 プログラムの中では  #include <sys/types.h>  #include <sys/stat.h>  using namespace std;   :  string aaa;   :  struct stat st;  if(stat(aaa,&st)!=-1){・・・   : という感じで記述しているので、 型が変換できない という感じのことを言っているのかなぁ・・・? と思っているのですが、間違いですか? また、間違えていないとしたら・・・、 このstringで宣言しているaaaをchar*(?)にキャストする方法 と言いますか、このエラーを解決する方法を教えてください。 毎度のことですが、理解不十分で、質問の意味が通じにくいかも しれませんが、どうか宜しくお願いいたします。m(_ _)m

質問者が選んだベストアンサー

  • ベストアンサー
  • MrBan
  • ベストアンサー率53% (331/615)
回答No.2

 if(stat(aaa.c_str(),&st)!=-1){・・・

mamappi82
質問者

お礼

即効、解決です。 ありがとうございました。m(___)m

その他の回答 (1)

  • koko_u_
  • ベストアンサー率18% (459/2509)
回答No.1

c_str()

mamappi82
質問者

お礼

回答ありがとうございました。 回答順ではkoko_uさんの方が早かったのですが、 No2.の方の回答には、 c_str()の使い方をまで記載してあったので、 ポイントを次点にさせていただきました。 ご了承くださいませ。 ありがとうございました。m(_ _)m

関連するQ&A

  • const char*のグローバル変数として使うのはどうしたら良いでし

    const char*のグローバル変数として使うのはどうしたら良いでしょうか? 以下のような動作を実装しようとしています。 mainからデータファイルの値を読み込みます。 その際、読み込むデータファイルのファイル名は別のコンフィグファイルに記載されているので、コンフィグから、データファイルのファイル名を読み込んで、データファイルをオープンする必要があります。 これをファイルを分割して実装しているのですがうまくいきません。 説明のため、データファイル名をmainから出力する実際と同じファイル構成のコードを作成しました。 以下のように5つのファイルに分けています。この際、データファイルのファイル名をconst char* filenameとしています。 ソースファイル #1: main.cpp //util.cppでfilenameに設定されたファイル名を表示する。 ソースファイル #2: set_filename.cpp //グローバル変数であるfilenameにコンフィグファイルに記載されているファイル名を設定する関数が定義されています。 ヘッダファイル #1: etc.h   //filenameの宣言が記載されていて、main.cpp, set_filenameでincludeされている。 ヘッダファイル #2: global.h //filenameの定義が記載されている。 ヘッダファイル #3 : set_filename.h //set_filename.cpp関数のプロトタイプ宣言 このような場合、set_filename.cpp内の関数で設定したfilenameがmain内で違う値に変わってしまいます。下のソースの例では、data.txtと出力されるのが期待値ですが、ゴミ値が表示されます。環境はVisual C++ 2008です。 どうして、set_filenameで設定したファイル名が変わってしまうのでしょうか? また、所望の動作はどのようにソースを変えれば実装できるのでしょうか? サンプルのソースは以下のとおりです。 -----main.cpp---------------- #include "etc.h" #include "global.h" #include "util.h" using namespace std; void main() { ifstream ifs("config.txt"); set_filename(ifs);  //config.txtには「data.txt」と記載しています。 cout<<filename<<endl; } -- ---------set_filename.cpp------- #include "etc.h" #include "util.h" void set_filename(ifstream &ifs) { string temp; ifs>>temp; filename=temp.c_str(); cout<<filename<<endl; ifs.close(); } ---- ---------etc.h----------------- #include <fstream> #include <cstdio> #include <string> #include <iostream> extern const char* filename;  //データファイル名です。 -- ----global.h-------- const char* filename; -- ----set_filename.h--- using namespace std; void set_filename(ifstream &ifs); --

  • System::String->char*変換でき

    C++/CLIでSystem::String^文字列をchar*に変換する関数を書いたのですが、256文字以上の文字列を投げて、戻値を確認してみると、255文字分しか中身が詰まっていません。元の文字列はアスキーのみです。 #include <msclr/marshal.h> using namespace msclr::interop; static char* toPtChar(const String^ Text) { String^ temp = (String^)Text; msclr::interop::marshal_context^ context = gcnew msclr::interop::marshal_context(); char* res = ((char*)(context->marshal_as<const char*>(temp))); return res; } この関数をどのように変更すれば、256文字以上変換できるでしょうか?

  • std::stringの継承

    #include <iostream> #include <string> class test : public std::string{ }; int main() { test tmp; tmp = "aaa"; } tmp = "aaa";ですが test::operator =(char *) が定義されていないとでますが何故なんでしょうか? string(basic_string)でoperator=が定義されていると思うのですが、 演算子の定義は継承されないのでしょうか。

  • System::Stringからconst charへの変換

    aという変数がSystem::String型であります。 そこで次に行いたい処理のために、const char*に変換したいのですが、下記のページを参考にプログラムしてみたのですがうまくいきませんでした。 うまくいかなかったというのはまだSystem::String型のままで変換できていなかったとエラーで出てしまいます。 そのような場合は、どのように型変換すればよいのでしょうか?

  • unsigned char SJis[2]からstd::stringに変換

    開発環境は VC++ 2008 Express Edition あるDLLの関数で戻り値としてShiftJISの1文字が格納された unsigned char SJis[2] が返され,これを呼び出し側のプログラムで使っている文字列 std::string str に順に追加していこうと思っています. そこで, unsigned char tmpSJis[3]; tmpSJis[0] = SJis[0]; tmpSJis[1] = SJis[1]; tmpSJis[2] = '\0'; str += std::string(tmpCode); というコードを書いてループさせたのですが, error C2440: '<function-style-cast>' : 'unsigned char *' から 'std::string' に変換できません。 というエラーが出てしまいうまく変換できません. これを解決する方法はありませんか?

  • std::wstringの継承

    #include<iostream> #include<string> #include <stdlib.h> #include <locale.h> #include <boost/lexical_cast.hpp> using boost::lexical_cast; using namespace std; VisualC++2008ExpressEditionで文字や数字を簡単に扱えるクラスを今作ろうとしていて以下のように作ってみました。 class multiString:public std::wstring{ public:   multiString(const wchar_t *ws){     /* multiString class自体に代入 */   } }; しかし、このwchar_tをクラスに代入する処理として、 multiString(const wchar_t *s)std::wstring(s); とすると、「error C2082: 仮パラメータ 's' が再定義されました。」となりますし、 multiString(const wchar_t *s)*this=s; とすると、例外が発生してしまします。 wstringだったら、簡単に代入処理として出来るのですが、継承した場合はどの様に実装すればいいのでしょうか? 宜しくお願いします。

  • stringについて

    C++初心者です。 このプログラムで続行するとエラーがでます。どうしたら無事実行することが出来るのでしょうか? #include<stdio.h> #include <iostream> using namespace std; int main(void) { string str("エラー"); cout << str<< endl; } エラー 1>c:\documents and settings\****\デスクトップ\zisyu12\zisyu12\main.cpp(58) : error C2679: 二項演算子 '<<' : 型 'std::string' の右オペランドを扱う演算子が見つかりません (または変換できません)。 . . . 以下省略

  • sys/types.hの必要性について

    #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int main (int argc,char *argv[]) { struct stat buf[2],*p;     if(argc!=3) { fprintf(stderr,"Usage:newer file1 file2\n"); exit(EXIT_FAILURE); } p=buf; if(stat(argv[1],p)<0) { perror("stat"); exit(EXIT_FAILURE); } p++; if(stat(argv[2],p)<0) { perror("stat"); exit(EXIT_FAILURE); } if(buf[0].st_mtime>buf[1].st_mtime) { printf("%s\n",argv[1]); } else { printf("%s\n",argv[2]); } return EXIT_SUCCESS; } 上記のプログラムでは<sys/types.h>を使っていますが、 どこの部分で必要になるのでしょうか? プログラム貼り付けて非常に見づらくてすいません

  • ディレクトリの判別

    FreeBSD5.4でstat()関数のS_ISDIRを使ってディレクトリかどうかを判別したいのですが、 S_ISDIRでは普通のファイルもディレクトリと認識してしまいます。もしご存知の方がいらっしゃいましたら、何卒ご教授くださいませ。 --------------------------------------------- #include <stdio.h> #include <string.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #include <stdlib.h> int createFileList(char *directoryName){ DIR *directoryId; struct dirent *directoryPointer; struct stat fi; directoryId=opendir(directoryName); while((directoryPointer=readdir(directoryId))!=NULL){ stat(directoryPointer->d_name,&fi); if (!S_ISDIR(fi.st_mode)) printf("%s\n",directoryPointer->d_name); } closedir(directoryId); return 0; } ---------------------------------------------

  • ファイル情報の「月」の値を整数値で取得するには?

    こんにちは。私は30代の男性です。 下記のサイトに載っていたコーディングなのですが、 http://okuyama.mt.tama.hosei.ac.jp/unix/C/slide93-1.html 「ctime(&filestat.st_mtime)」で取得した「Sat Apr 5」の「月」の値を整数で取得する方法を 考えています(例えば、「Sat 4 5」という感じで)。 何かいい方法はないでしょうか?アドバイスを頂けるとありがたいです。 よろしくお願いします。 #include <sys/types.h> /* stat(), struct stat */ #include <sys/stat.h> /* stat(), struct stat */ #include <stdio.h> /* fprintf(), printf() */ #include <errno.h> /* errno */ #include <string.h> /* strerror() */ #include <stdlib.h> /* exit() */ #include <time.h> /* time_t, ctime() */ int main(void) { struct stat filestat; char path[] = "/path/to/file"; time_t dtime; if(stat(path, &filestat) == -1) { fprintf(stderr, "* Error (%d) [stat: %s]\n", errno, strerror(errno)); exit(errno); } printf("Size: %ld\n", (long)filestat.st_size); printf("Last accessed: %ld, %s", filestat.st_atime, ctime(&filestat.st_atime)); printf("Last modified: %ld, %s", filestat.st_mtime, ctime(&filestat.st_mtime)); dtime = filestat.st_atime - filestat.st_mtime; printf("%ld\n", dtime); exit(0); } 実行例です。 Size: 86555 Last accessed: 1049485323, Sat Apr 5 04:42:03 2003 Last modified: 1049428803, Fri Apr 4 13:00:03 2003 56520

専門家に質問してみよう