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 38 39 40 41 42 43 44 45 46 47 | // 原始檔案(source.cpp) int main() { cout << "current line : " << _L_I_N_E_ << endl; cout << "current line : " << _L_I_N_E_ << endl; system("pause"); } // 前處理程式碼 int main() { const string lineString = "_L_I_N_E_"; ifstream ifs( "source.cpp" ); ofstream ofs( "destnation.cpp" ); string line; int lineCount = 1; while( getline( ifs, line ) ) { ostringstream oss; oss << '"' << lineCount++ << '"'; for( size_t index = 0; index = line.find( lineString, index ), index != string::npos; ++index ) { line.replace( index, lineString.length(), oss.str() ); } ofs << line << endl; } system("pause"); } // 結果檔案(destination.cpp) int main() { cout << "current line : " << "8" << endl; cout << "current line : " << "11" << endl; system("pause"); } |
Direct link: https://paste.plurk.com/show/287952