顯示具有 C++ 標籤的文章。 顯示所有文章
顯示具有 C++ 標籤的文章。 顯示所有文章

2011年3月9日 星期三

C/C++:計算指定資料夾的檔案數

int count_file_num_in_a_folder( char* target_folder ){
    int count=-1;                //檔案的counter
    char szDir[128];           //要讀取的資料夾的位址。 
    WIN32_FIND_DATA FileData;    //指著目前讀取到的File的指標。
    HANDLE hList;                //指著要讀取的資料夾的指標。
    sprintf(szDir, "%s\\*",target_folder );
    if ( (hList = FindFirstFile(szDir, &FileData))==INVALID_HANDLE_VALUE )
        cout<<"No directories be found."<<endl<<endl;
    else {
        while (1) {
            if (!FindNextFile(hList, &FileData)) {
                if (GetLastError() == ERROR_NO_MORE_FILES)
                    break;
            }
            count++;
        }
    }
    FindClose(hList);
    return count;
}
用途:
計算指定資料夾裡的檔案個數。
用法:
/*
char folder_adderss[128]="..\\testing_folder\\";
//testing_folder為指定資料夾的名稱
count_file_num_in_a_folder(folder_address);
*/
附註:
若testing_folder裡面有5個資料夾、3個檔案,則此函式會回傳5+3=8。而count會設成"-1"是因為FindFirstFile"會將[上層目錄]也是為檔案,因此若不想將[上層目錄]也視為檔案的話記得要將count設成"-1";否則,則設為0。

2011年1月27日 星期四

C/C++:FILE * fopen ( const char * filename, const char * mode )

"r"Open a file for reading. The file must exist.
"w"Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
"a"Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
"r+"Open a file for update both reading and writing. The file must exist.
"w+"Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
"a+"Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseekrewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.

2011年1月22日 星期六

C/C++:批次讀取指定資料夾內所有檔案的檔名

#include <windows.h>
#include <stdio.h>
void main()
{
    char InputPath[65535] = "C:\\Program Files\\";  
    //放要讀取檔案的資料夾路徑到InputPath字串裡
    char szDir[65535];
    char dir[65535];
    WIN32_FIND_DATA FileData;
    HANDLE          hList;
    sprintf(szDir, "%s\\*", InputPath );
    if ( (hList = FindFirstFile(szDir, &FileData))==INVALID_HANDLE_VALUE )
        printf("No files be found.\n\n");
    else {
        while (1) {
            if (!FindNextFile(hList, &FileData)) {
                if (GetLastError() == ERROR_NO_MORE_FILES)
                    break;
            }
            sprintf(dir, "%s\\%s", InputPath, FileData.cFileName);
            printf("%s\n", dir);
        }
    }
    FindClose(hList);
}

2011年1月20日 星期四

C/C++:好用的SIFT程式碼

作者:
程式:

原創論文:
原創程式:

[以下講解的是Rob Hess版本在Windows VC++的SIFT]
..\\SIFT\\dspFeat 為用來顯示SIFT feature的專案,開啟專案並compile後會在測試圖片上顯示代表SIFT的箭頭。 
顯示結果:
..\\SIFT\\match是用來顯示兩張圖在SIFT feature的比對情形的專案,相連的兩個點表示這兩個SIFT feature是一樣的。
顯示結果:



..\\SIFT\\siftFeat會顯示出SIFT在圖片上的分布情形,計算出在圖片上找到多少個SIFT點,並將SIFT的個數、維度、座標、向量都寫在檔案*.sift裡,方便日後要使用SIFT資料,不用重新計算SIFT,可以直接使用讀檔方式進行運用。

C/C++:Microsoft Visual Studio 2008使用OpenCV 2.1

OpenCV 2.1:點我下載(Microsoft Visual Studio 2008專用版)

Step 1.
安裝 Microsoft Visual Studio 2008。
Step 2.
安裝 OpenCV 2.1。
Step 3.
打開Microsoft Visual Studio 2008,選擇Tools -> Options -> Projects and Solutions -> VC++ Directories,在 "Show directories for:" 選擇Include files,加入目錄 C:\OpenCV2.1\include\opencv。在 "Show directories for:" 選擇Library files,加入目錄 C:\OpenCV2.1\lib,按下"OK",關閉Microsoft Visual Studio 2008。

以上安裝步驟參考這裡,因為OpenCV 2.1與以前版本有些不同,可以省略一些步驟。接著開始進行編寫自己的專案,請看Step 4。
Step 4.
請看這邊


Reference:
http://www.opencv.org.cn/index.php/%E9%A6%96%E9%A1%B5

2011年1月19日 星期三

C/C++:在指定路徑新增資料夾(_mkdir(), _rmdir() )

#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
   char path[_MAX_PATH]; // _MAX_PATH is defined in windows.h
   sprintf(path,"D:\\testtmp");
   if( _mkdir( path ) == 0 )
   {
      printf( "資料夾'D:\\testtmp'新增成功\n" );
      system( "dir D:\\testtmp" );
      system("pause");
      if( _rmdir( path ) == 0 )
        printf( "資料夾'D:\\testtmp'移除成功\n"  );
      else
        printf( "無法移除'D:\\testtmp'\n" );
   }
   else
     printf( "無法新增'D:\\testtmp'\n" );
     system("pause");
}

Reference:
http://msdn.microsoft.com/en-us/library/2fkk4dzw(v=VS.90).aspx