顯示具有 Coding 標籤的文章。 顯示所有文章
顯示具有 Coding 標籤的文章。 顯示所有文章

2012年2月6日 星期一

Windows programming, standard input/output - AllocConsole()

前言
整理一下,在 windows programming 的時候,如何使用 printf, 這類的 standard I/O (標準I/O) 來顯示訊息幫助開發?

原因
在 windows programming,使用 MFC 的標準 windows template 都沒有 console window 來幫助顯示開發訊息,當然可以使用 IDE 中 debug message, 但這邊提供另一種方式,因為個人偏好使用 printf 快速地傾印資訊。

解法

先貼一段程式碼,從網路上抄來的…

  AllocConsole();

  HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
  int hCrtOut = _open_osfhandle(/*(long)*/(intptr_t) handle_out, _O_TEXT);
  FILE* hf_out = _fdopen(hCrtOut, "w");
  setvbuf(hf_out, NULL, _IONBF, 1);
  *stdout = *hf_out;

  HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
  hCrtIn = _open_osfhandle(/*(long)*/(intptr_t) handle_in, _O_TEXT);
  FILE* hf_in = _fdopen(hCrtIn, "r");
  setvbuf(hf_in, NULL, _IONBF, 128);
  *stdin = *hf_in;


然後再補上 streaming(串流) 的關係圖


這樣搭配看 應該很好理解程式碼。

補充說明
Process / Windows 就是目前在開發的 GUI 程式。

*stdout / *stdin 就是 CRT(C Run Time) lib 中的標準輸入跟輸出的指標,系統制訂的。

Console window 是使用 Window API, AllocConsole() 所產生。
搭配使用 GetStdHandle() 來取得 console standard I/O handle, handle_in / handle _out。

透過 _open_osfhandle() 將 console standard input/output handle 轉換為 file handle。
再使用 fdopen() 的方式,把 file handle, hCrtIn / hCrtOut 轉換為 file description。

最後,把 file description, hf_in / hf_out assigned to *stdin / *stdout 。
這樣視窗開發時,使用 printf 都不是問題囉!

有問題再留言囉!謝謝。

2009年7月14日 星期二

VIM search & replace tips 好用技巧(ctrl+r)

文章出處:http://drakeguan.org/content/vim-%E7%9A%84-replace-by-search

用了 VIM 也一段時間了,原來 search(搜尋) & replace(取代) 功能是這麼地方便使用。

  1. 使用 /<放上你的正則運算式> 此時應該可以在內文上反白已搜尋之目標。
  2. 打上 : 進入 command mode 輸入 s/" 然後按下 ctrl+r ,
  3. 按下 / 此時剛剛的搜尋(正則運算式)就自動地被貼上來。
  4. 再來就是取代字串囉!
  • %s/<>/<>/gc 可以搜尋整隻檔案,並在取代的過程會提示你是否要取代。
大家一起來用 VIM 吧!

多補充個搜尋的小技巧吧!怎麼樣弄出像UltraEdit 一樣的搜尋結果清單呢?
很簡單 ~~

%g/<pattern>/z#=<num>

<pattern> 為搜尋的字串。
<num> 為該行上下個<num>的顯示。