CソースとC++ソース共存実験(その1)

CソースとC++ソース共存実験(その1)

CソースとC++ソースの共存を実験してみた。 とは言っても、printf()とcout<< を同時に使ってみただけ。 同時に使えるんですね。まずは、第一歩。 $ cat c_cpp_test.cpp #include #include using namespace std; int main() { printf(“Hello, world\n”); cout << “Hello, world2\n”; return 0; } $ g++ c_cpp_test.cpp $ ./a.out Hello, world Hello, world2 $ 続きがあるのか?は未定w

2010-08-08 · 1 分 · Masayuki Igawa
SQLiteでprepared statementとBLOBを使って構造体を保存(C言語)その1 #sqlite

SQLiteでprepared statementとBLOBを使って構造体を保存(C言語)その1 #sqlite

SQLiteのC言語APIを使ってprepared statementで、BLOBを使ってみます。 基本的には、以下のサイトに書いてある方法ですが、日本語で書いてみることにします。 http://www.sqlite.org/capi3ref.html#sqlite3_stmt sqlite3_prepare_v2()(あるいはそれに類するもの)を使って、sqlite3_stmtのオブジェクトを作る。 sqlite3_bind_*() インターフェースを使ってパラメータに値をBind。 sqlite3_step()を使ってSQL実行。(sqlite3_step()は何回でも実行できる) sqlite3_reset()を使ってstatementをリセットして、step 2に戻る。これは何度でもできるし、やらなくても良い。 sqlite3_finalize()を使ってオブジェクトを破棄する。 というわけで、いきなりサンプルコードw 便利だなーと思ったのは、sqlite3_errmsg()関数。dbを引数にすると、 $ gcc -o sqlite_test sqlite_test.c -lsqlite3 $ chmod 000 test.db と、DBファイルを読み書き出来ないようにして、実行すると、 $ ./sqlite_test open error:14, unable to open database file と、何でエラーになったのかが表示されます。 さらに、このDBファイルをsqlite3コマンドで見たときの話は次回のエントリに書きたいと思います。 #include #include #include #include struct human { char name[256]; int age; int height; int sex; }; int main(int argc, char *argv[]) { struct human human[5] = { { "Isono Katsuo", 32, 168, 0 }, { "Isono Wakame", 29, 158, 1 }, { "Huguta Tarao", 20, 180, 0 }, { "Huguta Masuo", 52, 178, 0 }, { "Huguta Sazae", 48, 161, 1 } }; sqlite3 *db; sqlite3_stmt *dropStmt = NULL; sqlite3_stmt *createStmt = NULL; sqlite3_stmt *selectStmt = NULL; sqlite3_stmt *insertStmt = NULL; char *drop_tbl_sql = "drop table member"; char *create_tbl_sql = "create table member" "(id INTEGER PRIMARY KEY," "human BLOB NOT NULL )"; char *insert_tbl_sql = "INSERT INTO member (id, human) values (?, ?)"; char *select_tbl_sql = "select human from member where id = ?"; char *pzTail; int rc = 0; int exitcode = 0; rc = sqlite3_open("test.db", &db); //rc = sqlite3_open(":memory:", &db); if (rc != SQLITE_OK) { printf("open error:%d, %s\n", rc, sqlite3_errmsg(db)); exitcode = 1; goto end; } /* cleanup. ignore error. */ sqlite3_prepare_v2(db, drop_tbl_sql, -1, &dropStmt, NULL); sqlite3_step(dropStmt); rc = sqlite3_prepare_v2(db, create_tbl_sql, -1, &createStmt, NULL); if (rc != SQLITE_OK) { printf("create error:%d, %s\n", rc, sqlite3_errmsg(db)); exitcode = 1; goto end; } rc = sqlite3_step(createStmt); if (rc != SQLITE_DONE) { printf("create error:%d, %s\n", rc, sqlite3_errmsg(db)); exitcode = 1; goto end; } rc = sqlite3_prepare_v2(db, insert_tbl_sql, -1, &insertStmt, NULL); if (rc != SQLITE_OK) { printf("insert error:%d, %s\n", rc, sqlite3_errmsg(db)); exitcode = 1; goto end; } rc = sqlite3_prepare_v2(db, select_tbl_sql, -1, &selectStmt, NULL); if (rc != SQLITE_OK) { printf("select error:%d, %s\n", rc, sqlite3_errmsg(db)); exitcode = 1; goto end; } int i = 0; for (i = 0; i < 5; i++) { sqlite3_reset(insertStmt); sqlite3_bind_int(insertStmt, 1, i); sqlite3_bind_blob(insertStmt, 2, &human[i], sizeof(struct human), SQLITE_STATIC); rc = sqlite3_step(insertStmt); if (rc != SQLITE_DONE) { printf("insert error:%d, %s\n", rc, sqlite3_errmsg(db)); exitcode = 1; goto end; } } printf("-------------------------------------\n"); for (i = 0; i < 5; i++) { sqlite3_reset(selectStmt); sqlite3_bind_int(selectStmt, 1, i); rc = sqlite3_step(selectStmt); //printf("select result:%d\n", rc); if (rc == SQLITE_ROW) { struct human *t = (struct human*)sqlite3_column_blob(selectStmt, 0); printf("ID: %d\n\t name: %s\n\t age: %d\n\t height: %d\n\t sex: %s\n", i, t->name, t->age, t->height, t->sex == 0 ? "M" : "F"); printf("-------------------------------------\n"); } } end: sqlite3_finalize(selectStmt); sqlite3_finalize(insertStmt); sqlite3_finalize(createStmt); sqlite3_finalize(dropStmt); return exitcode; }

2010-06-19 · 2 分 · Masayuki Igawa
C/C++でGC

C/C++でGC

Java野郎(私)が、C/C++でメンドイものの一つに、 **「malloc()したらfree()しなきゃならん」**というものがある。 大体、誰も参照しなくなったら、もはや、誰も触れないんだから、勝手に開放してほしいのだ。 でも、それをC言語自体に望むのは無理だって言うのもわかってる。 どうにかなんないの?と思っていたら、C/C++でGCするためのライブラリがあるということを知りました。 http://www.hpl.hp.com/personal/Hans_Boehm/gc/ 自分(個人)でプログラム書くときは、これ使います。多分。 (いちいち、malloc()/free()なんてしてられっか)

2008-07-07 · 1 分 · Masayuki Igawa
独習C++ 改訂版日記 P.52 練習問題2.2 2.〜

独習C++ 改訂版日記 P.52 練習問題2.2 2.〜

2.(こんなコードで良いのか?) #include #include using namespace std; class t_and_d { time_t now_time; public: t_and_d(time_t _now_time); void disp(); }; t_and_d::t_and_d(time_t _now_time) { now_time = _now_time; } void t_and_d::disp() { struct tm *tm = localtime(&now_time); char *time_str = asctime(tm); printf("%s\n", time_str); } int main() { time_t n = time(NULL); t_and_d td(n); td.disp(); return 0; } 3.(こんなコードで良いのか?) #include using namespace std; class box { double height; double width; double breadth; public: box(double _height, double _width, double _breadth); void vol(); }; box::box(double _height, double _width, double _breadth) { height = _height; width = _width; breadth = _breadth; } void box::vol() { printf("vol:%f\n", height * width * breadth); } int main() { box b(10.0, 20.0, 30.0); b.vol(); return 0; }

2008-07-04 · 1 分 · Masayuki Igawa
独習C++ 改訂版日記2

独習C++ 改訂版日記2

というわけで、P.52 練習問題2.2 1. #include using namespace std; class stack { char *stck; int tos; char who; int size; public: stack(char c, int _size); void push(char ch); char pop(); }; stack::stack(char c, int _size) { size = _size; stck = (char *)malloc(size); tos = 0; who = c; cout << "Constructing stack " << who << "\n"; } void stack::push(char ch) { if (tos == size) { cout << "Stack " << who << " is full\n"; return; } stck[tos] = ch; tos++; } char stack::pop() { if (tos == 0) { cout << "Stack " << who << " is empty\n"; return 0; } tos--; return stck[tos]; } int main() { int i; int size1, size2; cout << "Enter two sizes: "; cin >> size1 >> size2; stack s1('A', size1), s2('B', size2); s1.push('a'); s2.push('x'); s1.push('b'); s2.push('y'); s1.push('c'); s2.push('z'); for (i = 0; i < 5; i++) { cout << "Pop s1: " << s1.pop() << "\n"; } for (i = 0; i < 5; i++) { cout << "Pop s2: " << s2.pop() << "\n"; } return 0; }

2008-07-02 · 1 分 · Masayuki Igawa
独習C++ 改訂版日記

独習C++ 改訂版日記

「独習C++ (改訂版)を(本自体は、ずいぶん前に買ったんですが)やり始めているので、書いていくことにしてみようと思う。(いつまで続くやらw) いきなりだが、P.46 練習問題2.1から。 キュークラスを改良して、初期化関数をコンストラクタに置き換え。 stopwatchクラスの作成 次に示すコンストラクタの誤りは?

2008-06-29 · 1 分 · Masayuki Igawa