VC++ (main.cpp):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #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; } |
說明:
將檔案拖曳至exe執行檔,並藉由argc、argv取得路徑,轉換路徑變數型態後進行讀檔動作。
Batch檔是用來測試多行參數的讀取,須放在與執行檔相同之目錄。
沒有留言:
張貼留言