Show toolbar
顯示具有 argc argv 標籤的文章。 顯示所有文章
顯示具有 argc argv 標籤的文章。 顯示所有文章

2013年7月15日 星期一

Exception Handling

標題:例外處理的方法
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <regex>
#include <fstream>
using namespace std;

class pos
{
public:
    float x;
    float y;
    float z;
};

class ERR
{
private:
    int kind;
public:
    ERR(int k) : kind(k){}; //建構函式
    char* message()
    {
        switch(kind)
        {
        case 0:
            return "Error A";
        case 1:
            return "Error B";
        case 2:
            return "Error C";
        }
    }
};

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

int main(int argc, char *argv[])
{
    switch(0) //Select Example
    {
    case 0:
        {
            //Example 1
            int n = 0;
            string str = "Hello";
            try
            {
                cin >> n;
                cout << str.at(n) << endl; //取得字串的第n個字元
            }
            catch (...)
            {
                cout << "Error!" << endl;
            }
            break;
        }
    case 1:
        {
            //Example 2
            float num = 1;
            try
            {
                cout << "num=";
                cin >> num;
                if(num == 0)
                {
                    throw "Infinity!";
                }
                cout << "100/num=" << 100/num << endl;
            }
            catch (const char* message)
            {
                cout << message << endl;
            }
            break;
        }
    case 2:
        {
            //Example 3
            try {
                if(argc<2)
                {
                    throw "Parameter error.";
                }
                char pathChar[1024];
                regex pathRegex("([a-zA-Z]\:\\\\(?:[^\\\\]+\\\\)*.*)"); //"C:\\temp\\hello.txt"
                cmatch pathMatch; //cmatch: Char Match
                strcpy(pathChar, argv[1]); //Pointer to Char
                if(regex_match(pathChar, pathMatch, pathRegex)) //char, cmatch, regexp
                {
                    readfile(pathMatch[1].str());
                }
                else
                {
                    throw "Path Error.";
                }
            }
            catch(const char* message)
            {
                cout << message << endl;
            }
            break;
        }
    case 3:
        {
            //Example 4
            pos p;
            p.x = 1.1f;
            p.y = 2.2f;
            p.z = 3.3f;
            cout << "(" << p.x << ", " << p.y << ", " << p.z << ")" << endl;
            break;
        }
    case 4:
        {
            //Example 5
            try
            {
                throw ERR(0);
            }
            catch(ERR e)
            {
                cout << e.message() << endl;
            }
            break;
        }
    }

    system("pause");
    return 0;
}

說明:
一般及搭配argc、argv、Regular Expression、Class例外處理的方法

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檔是用來測試多行參數的讀取,須放在與執行檔相同之目錄。