コマンドライン引数
http://wisdom.sakura.ne.jp/system/winapi/win32/win6.html
#include<windows.h>
int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance ,
PSTR pCmdLine , int nCmdShow ) {
MessageBox(NULL , GetCommandLine() ,
TEXT("Kitty") , MB_ICONINFORMATION);
return 0;
}
http://www.wisdomsoft.jp/dev/api/windows/005
[GetCommandLine() 関数]
この関数は、起動時のコマンドライン引数を返します。
LPTSTR は、TCHARのポインタ型なので、
この関数から得られる文字列はマルチバイトに対応しています。
Unicode に統一しているアプリケーションなどは、
こちらの関数から取得した方が都合が良いでしょう。
///////////////
http://www7a.biglobe.ne.jp/~lshen/EternalWindows/WinBase/Window/Window06.html
プログラムをUNICODEとしてコンパイルしている場合に限りますが、
CommandLineToArgvWを利用したコマンドライン引数の取得方法もあります。
//////////////////
http://homepage1.nifty.com/nogue/wincho16.htm
コマンドラインの分割処理 C++ソースあり
///////////
http://www11.ocn.ne.jp/~ikalu/cplus/5001.html
C++, STL 基礎的な覚え書き(サンプルソース)
////////////
第15回 「送る」メニューから画像を開く
http://www.usefullcode.net/2006/12/15.html
///////////////////////////////////
int fileinput(){
ifstream fin;//ファイルストリーム
char cIn;//入力文字
//.txt を入力モードで開く
fin.open( "新規テキスト ドキュメント.txt", ios::in);//ios::inはテキストモード入力
if( !fin ){ return -1; }
while(1){
fin.get(cIn); //1文字づつ入力
if( fin.eof() ){
cout << "EOF!!\n" << endl;
break; } //ファイルの終わりに達したらwhileを抜ける }
//ここに処理を書く
cout << cIn; //これだと余計な改行が出力されない
/*
123
abc
+*<
/-\
!"'EOF!!
*/
}
fin.close();
return 0;
}
■ ファイルから文字を入力するには
http://www11.plala.or.jp/studyhall/cpp/cpp.html
////////////////////////////////////
■ファイルに文字を出力するには (2通り)
http://www11.plala.or.jp/studyhall/cpp/cpp.html
int fileoutput(){
ofstream fout;
char sOut[256] = "出力文字列";
//.txtを出力モードで開く
fout.open( "新規出力テキスト.txt", ios::out );//binaryモードはios::binary
if( !fout ){ return -1; }//ファイルオープンエラーの時
//fout << sOut << '\n';
fout << sOut;//こちらでも良い
fout.close();
return 0;
}
int fileoutput2(){
ofstream fout;
char sOut[256] = "出力文字列です。";
fout.open("新規出力テキスト.txt", ios::out);
if(!fout){return -1;}
int tmp_length = strlen(sOut);
for(int i=0; i<tmp_length; i++ ){
fout.put(sOut[i]);
}
return 0;
}
■ファイルが存在するか確認するには
http://www11.plala.or.jp/studyhall/cpp/cpp.html
int file_chk(){
//////////
char dir[_MAX_PATH];
GetCurrentDirectory(_MAX_PATH, dir);
//cout << dir;
//c:\Documents and Settings\fuse\My Documents\Visual Studio Projects\temp_ (\と改行無し)
//////////
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
string f, dir_f;
f = "\\新規出力テキスト.txt";
dir_f = dir;
dir_f += f;
cout << dir_f.c_str() << endl;
hFind = FindFirstFile( dir_f.data(), &FindFileData);
if( hFind == INVALID_HANDLE_VALUE ){ cout << "File not found." << endl; }
else{ cout << "File found." << endl; }
return 0;//これでFile found.と出る
}
◆単純な文字列の置換
http://www.s34.co.jp/cpptechdoc/aftercare/art_24_01.html
#include <iostream>
#include <string>
template<class E, class T, class A>
std::basic_string<E,T,A>
replace_all(
const std::basic_string<E,T,A>& source, // source中にある
const std::basic_string<E,T,A>& pattern, // patternを
const std::basic_string<E,T,A>& placement // placementに置き換える
) {
std::basic_string<E,T,A> result(source);
for ( std::string::size_type pos = 0 ;
std::string::npos != (pos = result.find(pattern,pos));
pos += placement.size() )
result.replace(pos, pattern.size(), placement);
return result;
}
int main() {
std::string source = "すうどんもうどんもうどんのうち" ;
std::string pattern = "うどん" ;
std::string placement = "もも" ;
// 結果 : "すもももももももものうち"
std::cout << replace_all(source,pattern,placement) << std::endl;
return 0;
}
◆文字列切り分け <map>使用
http://cham.ne.jp/piro/cpp_sample.html#split
◆実行ファイルがあるディレクトリ取得
char filename[_MAX_PATH];
GetModuleFileName(NULL, filename, _MAX_PATH);
■CreateProcess()
http://www11.plala.or.jp/studyhall/cpp/system.html
int c_process( string &st_cmdline ){...}
みたいにstringを引数にしたときはCreateProcessにおいて
(LPSTR)st_cmdline.c_str(), //LPSTRにキャストすること
こうしないと、VC++でのコンパイル時に、
「error C2664: const char* から LPSTR に変換できません」
というエラーが出る。
◆iostreamとstdio.hを同時に使うときは
std::ios::sync_with_stdio();
をmainの直後に呼ぶ必要があるらしい。
最終更新:2008年01月17日 11:06