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

2013年7月15日 星期一

Drag to read the file

標題:拖曳方式讀取檔案
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <fstream> //ifstream
#include <sstream> //getline
using namespace std;

int main(const int argc, char *argv[])
{
    /*int i = 0;
    cout << "argc: " << argc << endl;
    for(i=0;i<argc;i++)
    {
        cout << "*argv[" << i << "]: " << argv[i] << endl;
    }*/

    //Path Convert
    char pathChar[1024];
    string pathString = "";
    strcpy(pathChar, argv[1]); //Pointer to Char
    pathString.assign(pathChar); //Char to String

    //ReadFile
    string lines = "";
    ifstream File(pathString);
    if (File.is_open()) //Check File
    {
        while(File.good())
        {
            getline(File, lines);
            cout << lines << endl;
        }
        File.close();
    }

    system("pause");
    return 0;
}
Batch (Start.bat):


說明:
將檔案拖曳至exe執行檔,並藉由argc、argv取得路徑,轉換路徑變數型態後進行讀檔動作。
Batch檔是用來測試多行參數的讀取,須放在與執行檔相同之目錄。

2013年7月10日 星期三

Get value using Regular Expression

標題:C/C++使用正規表示法取值
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <regex> //string lib here

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "--- Example 1 ---" << endl;
    //String Match
    string strA = "13393774";
    smatch matchA;
    regex regA("[0-9]+");
    if(regex_match(strA, matchA, regA))
    {
        cout << atof(matchA[0].str().c_str()) << " is number." << endl;
    } else {
        cout << "Not a number." << endl;
    }

    cout << "--- Example 2 ---" << endl;
    //String Match
    string strB = "-4.472136e-001";
    smatch matchB;
    regex regB("[+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?");
    if(regex_match(strB, matchB, regB))
    {
        cout << atof(matchB[0].str().c_str()) << " is number." << endl;
    } else {
        cout << "Not a number." << endl;
    }
    
    cout << "\n--- Example 3 ---" << endl;
    //String Search
    string strC = "   facet normal 0.000000e+000 -4.472136e-001 8.944272e-001";
    smatch matchC;
    regex regC(" *facet normal \
([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?) \
([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?) \
([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?)");
    if(regex_match(strC, matchC, regC))
    {
        cout << "Facet normal: (";
        cout << atof(matchC[1].str().c_str()) << ", ";
        cout << atof(matchC[2].str().c_str()) << ", ";
        cout << atof(matchC[3].str().c_str()) << ")" << endl;
    } else {
        cout << "Not Match." << endl;
    }

    return 0;
}

說明:
在C/C++中使用正規表示式取得數值。

2013年7月8日 星期一

Using Regular expression in C/C++

標題:C/C++使用正規表示法
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <regex> //string lib here

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    //String Match
    string strA = "1337";
    regex regA("[0-9]+");
    if(regex_match(strA, regA))
    {
        cout << "A Match." << endl;
    } else {
        cout << "A Not Match." << endl;
    }

    //String Search
    string strB = "cad23342";
    regex regB("[0-9]");
    if(regex_search(strB, regB))
    {
        cout << "B Match." << endl;
    } else {
        cout << "B Not Match." << endl;
    }

    //String Replace
    string strC = "Hello I'm QQBoxy!!";
    regex regC(" ");
    string wordC = "\n";
    cout << regex_replace(strC, regC, wordC) << endl;

    //String Replace
    string strD = "Hello I'm QQBoxy!!";
    regex regD("a|e|i|o|u");
    string wordD = "[$&]";
    cout << regex_replace(strD, regD, wordD) << endl;

    return 0;
}
說明:
在C/C++中正規表示式常用的三種語法。

Two ways to Read and Write Files

標題:C/C++兩種讀取文字檔的方法
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
//#include <iomanip>
//setprecision(4) "3.1415"
//setfill('Q') "QQ123"
//setw(5) "  123"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int i = 0, j = 0;
    const char *filePath = "pi.xls";

    remove(filePath); //Delete File
    
    //VC++ -----------------------------------------------------

    //Write File
    ofstream outputFile(filePath, fstream::app); //fstream::out fstream::app
    if (outputFile.is_open()) //Check File
    {
        for(j=1;j<10;j++)
        {
            for(i=1;i<10;i++)
            {
                outputFile << i << "*" << j << "=" << i*j << "\t";
            }
            outputFile << endl;
        }
        outputFile.close();
    }

    //Read File
    string lines = "";
    ifstream inputFile(filePath);
    if (inputFile.is_open()) //Check File
    {
        while(inputFile.good())
        {
            getline(inputFile, lines);
            cout << lines << endl;
        }
        inputFile.close();
    }

    //Standard C -----------------------------------------------------

    //Write File
    FILE *writeFile = fopen(filePath, "a");
    if (writeFile) { //Check File
        for(j=1;j<10;j++)
        {
            for(i=1;i<10;i++)
            {
                fprintf(writeFile, "%d*%d=%d\t", i, j, i*j);
            }
            fprintf(writeFile, "\n");
        }
        fclose(writeFile);
    }
    
    //Read File
    char line[100] = ""; // Light!!
    FILE *readFile = fopen(filePath, "r");
    if (readFile) { //Check File
        while(fgets(line, sizeof(line), readFile))
        {
            printf("%s", line);
        }
        fclose(readFile);
    }

    return 0;
}

說明:
使用fstream的方法讀寫檔案,以及使用C的fopen讀寫檔案的方法。

2013年4月25日 星期四

Verify a character is in English or number

標題:使用ASCII判斷字元是英文或數字
C (main.html):
#include <stdio.h>
#include <string.h> //供strlen使用

int main(void)
{
    int i = 0;
    char str[9] = "_09azAZ?";
    for(i=0;i<strlen(str);i++) //檢查字串中的字元是否均為英文
    {
        //使用十進制ASCII碼判斷字元
        if(str[i] >= 48 && str[i] <= 57) //數字的ASCII碼落在48 ~ 57
        {
            printf("%c 是數字\n", str[i]);
        }
        else if (str[i] >= 65 && str[i] <= 90) //大寫的ASCII碼落在65 ~ 90
        {
            printf("%c 是大寫\n", str[i]);
        }
        else if (str[i] >= 97 && str[i] <= 122) //小寫的ASCII碼落在97 ~ 122
        {
            printf("%c 是小寫\n", str[i]);
        }
        else //其它非英數
        {
            printf("%c 非英數\n", str[i]);
        }
    }
    return 0;
}


說明:
使用ASCII碼十進制判斷字串中的字元是英文或數字,其中注意到字元陣列大小為8(字元數)+1(字串結束符)=9。

2011年7月20日 星期三

PHP exec C

標題:PHP與C相互傳遞與接收
程式:
PHP (index.php):
<?php
//傳遞變數給exe執行檔
$text = exec("chello\\chello.exe Hello PHPBoxy", $arr);
 
//印出陣列大小
echo count($arr)."<br /><br />";
 
//印出陣列結果
foreach($arr as $value)
    echo $value."<br />";
 
//僅印出最後一行
echo "<br />".$text;
?>

C (chello\chello.exe):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv) {
    char str1[1024], str2[1024], str3[1024];
    if(argc > 1) {
        strncpy(str1,argv[0],sizeof(str1)-1);
        strncpy(str2,argv[1],sizeof(str2)-1);
        strncpy(str3,argv[2],sizeof(str3)-1);
        printf("%s\n%s\n%s", str1, str2, str3);
    } else {
        printf("%d\n", argc);
    }
    return (EXIT_SUCCESS);
}

輸出結果:


說明:
利用PHP傳遞『Hello』與『PHPBoxy』兩個字串給執行檔接收,
再經由C/CPP程式printf回傳給PHP三行字串內容,
注意當argv需要做字串的處理時必須要重新複製一份來進行修改,
若直接使用argv來做修改可能會導致資料錯誤的發生。