2014年11月12日 星期三

C 標準函數庫 - time.h mktime() 時間轉秒數

轉自 http://pydoing.blogspot.tw/2010/07/c-mktime.html

time.h 的函數 mktime() 以指向結構 tm 的指標當作參數,回傳此 tm 所表示的日曆時間。

以下程式示範使用 mktime() 的結果

#include < stdio .h >
#include < time .h >
 
int main(void)
{
    time_t n;
    struct tm t1;
    t1.tm_sec = 8;
    t1.tm_min = 12;
    t1.tm_hour = 3;
    t1.tm_mday = 22;
    t1.tm_mon = 3;
    t1.tm_year = 1999-1900;
    t1.tm_wday = 1;  //可省略
    t1.tm_yday = 81;  //可省略
    t1.tm_isdst = -1;  //可省略
     
    n = mktime(&t1);
    printf("西元 1999 年 3 月 22 日 3 點 12 分 8 秒共累計了 %u 秒....\n", n);
     
    return 0;
}
 
/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:cmktime.c
    功能:示範 time.h 中函數 mktime() 的使用
    作者:張凱慶
    時間:西元2010年6月 */

2014年10月28日 星期二

術語「參數」(Parameter) 與「引數」(Argument) 的區別


一個「參數」(Parameter) 代表一個值,您必須在呼叫程序時傳遞該參數。 程序的宣告會定義其參數。
當您定義 Function 或 Sub 程序時,會在緊接著程序名稱之後的括號中指定「參數清單」(Parameter List)。 您會針對每個參數,指定名稱、資料型別和傳遞機制 (ByVal (Visual Basic) 或 ByRef (Visual Basic))。 您也可以指定某個參數為選擇性參數, 這表示呼叫程式碼不必將值傳給該參數。
每一個參數的名稱都可做為程序內部的「區域變數」(Local Variable)。 您可以使用任何其他變數的相同方式來使用參數名稱。

引數」(Argument) 代表您在呼叫程序時會傳給程序參數的值。 呼叫程式碼會在呼叫程序時提供引數。
呼叫 Function 或 Sub 程序時,會在緊接著程序名稱之後的括號中包含「引數清單」(Argument List)。 每一個引數會對應至清單中相同位置的參數。
與參數定義不同的是,引數沒有名稱。 每個引數都是一個運算式,可包含零或多個變數、常數和常值 (Literal)。 評估運算式的資料型別,通常應該會符合為對應參數所定義的資料型別,且不論在任何情況下,都必須能將它轉換成參數型別。

簡單的說
定義時稱為「參數」(Parameter) 
ex:
void myFunction(int i){   // i 為參數
//ToDo...
}

呼叫時稱為「引數」(Argument)
ex:
int i = 10;
myFunction(i);   // i 為引數

2014年9月25日 星期四

C++ 字串切割

Arguments AnalyticArguments(int argc, _TCHAR* argv[])
{
    //初始化
    Arguments args;
    args.strAction = "";
    args.strVMName = "";
    args.strSnapshotName = "";

    if(argc >= 3)
    {
        //歷遍所有參數
        for(int i=0 ; i -1)
        {
            //取得一個參數 ex:/Action=start
            string strArgument = argv[i];
            //初始化
            int iTok = -1;
            //=========================判斷參數keyword=========================
            if((iTok = strArgument.find("/Action")) > -1)
            {
                iTok = strArgument.find("=");
                int strLength = strArgument.length();
                //判斷等號右邊是否有文字
                if(iTok > -1)
                    args.strAction = strArgument.substr(iTok+1, strLength);
            }
            else if((iTok = strArgument.find("/VMName")) > -1)
            {
                iTok = strArgument.find("=");
                int strLength = strArgument.length();
                //判斷等號右邊是否有文字
                if(iTok > -1)
                    args.strVMName = strArgument.substr(iTok+1, strLength);
            }
            else if((iTok = strArgument.find("/SnapshotName")) > -1)
            {
                iTok = strArgument.find("=");
                int strLength = strArgument.length();
                //判斷等號右邊是否有文字
                if(iTok > -1)
                    args.strSnapshotName = strArgument.substr(iTok+1, strLength);
            }
            //=================================================================
        }
    }
    return args;
}

C++ 讀文字檔

list lisVMNames;
char line[100];
fstream fin;
//讀檔
fin.open("D:\\Joyce\\TXTs\\BigFarmer.txt",ios::in);
while(fin.getline(line,sizeof(line),'\n'))
{
    //將一行文字存入list
    lisVMNames.push_back(line);
}

2014年9月22日 星期一

C# 字串切割

string[] strParameter = args[i].Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);

以範例來說,以等號切割字串後,由字串陣列接收

關鍵字可用string,而不僅限於char

2014年9月9日 星期二

C# 列出目錄及子目錄之所有檔案或特定檔案

轉貼自:http://note.tc.edu.tw/446.html
C# 列出目錄及子目錄之所有檔案

用遞迴的方法,列出目錄及子目錄之所有檔案。以下的方法如果目錄或檔名太長會出錯。
using System.IO;
using System.Collections;

private void GetFiles(DirectoryInfo di, string searchPattern, ref ArrayList MyFiles)
{
     //取得檔案
    foreach (FileInfo fi in di.GetFiles(searchPattern))
    {
        MyFiles.Add(fi.FullName);
    }

    // Search in subdirctories
    foreach (DirectoryInfo d in di.GetDirectories())
    {
        GetFiles(d, searchPattern, ref MyFiles); //遞迴方法
    }
}

VisualStudio 2012 CrystalReport 無法載入crdb_adoplus.dll

轉貼來自:
http://fms-dot-net-notes.blogspot.tw/2011/09/crystalreport2010-crdbadoplusdll.html

在VS2012中使用CrystalReport 會出現一個錯誤
發生"無法載入檔案或組件’file:///C:\Program Files\SAP BusinessObject\Crystal Report for .Net Framework 4.0\Common\SAP BusinessObject Enterprise XI 4.0\win32_x86\donet1\crdb_adoplus.dll’或其相依性的其中之一系統找不到指定的檔案"
奇怪的是,裝好CrystalReport後卻沒有 donet1 這個資料匣!!
crdb_adoplus.dll 這個檔案卻是在 win32_x86 這個資料匣下...... XD

解決方式就是在 C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86 目錄下新增一個資料匣 dotnet1
再把 crdb_adoplus.dll 複製過去

好........
執行後就會發現又出現了另一個錯誤~~
混合模式組件是針對版本 ‘v2.0.50727′ 的執行階段建置的,無法在沒有其他組態資訊的情況下載入 4.0 執行階段中。

解決方式~
如果是 Web 就開啟 web.config 如果是 WinForm 就開啟 app.conifg
找到
< startup >
    < supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" / >
< /startup >

並在 startup 加上 useLegacyV2RuntimeActivationPolicy="true"

< startup useLegacyV2RuntimeActivationPolicy="true" >
    < supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" / >
< /startup >

2014年9月4日 星期四

C++ Command Line 傳入參數方法

#include < iostream >

using namespace std;  
  
int main(int argc,char **argv){  
  
    for(int i=0;i < argc ; i++)  
        cout << "Argument " << i << " is " << argv[i] << endl;  
    return EXIT_SUCCESS;  
}  

當透過Dos模式呼叫test.exe時,如果需要傳入參數,可傳入以下指令

test.exe hello! this is a test!

結果如下:

2014年8月28日 星期四

2014年8月24日 星期日

MFC 字串切割

//========================================
//mapKey、*liAtkIP為一組IP+Port
//將port移除只保留IP
char szBuf[100];
char mapKeyIP[100];
const char *del = ":";
    
strcpy(szBuf, (LPCTSTR)*liAtkIP);
*liAtkIP = strtok( szBuf, del);
    
strcpy(mapKeyIP, (LPCTSTR)mapKey);
mapKey = strtok( szBuf, del);
//=========================================