C++模擬Linux Shell編寫一個(gè)自定義命令
本文將根據(jù)C++模擬Linux Shell寫一個(gè)自定義命令,下面是示例代碼,需要的可以參考一下
示例代碼
#include <iostream> #include <fstream> #include <string> #include <vector> #include "stdarg.h" using namespace std; #define MAXLEN 200 void write2File(const std::string &file_string, const std::string str_content); void readFromFile(string file_string); string getParamStr(char *ch_pt); void ProcessByPattern(char *ptr, ...); /*如果不存在file,則會(huì)在當(dāng)前文件夾下創(chuàng)建file*/ void write2File(const std::string &file_string, const std::string str_content) { if (file_string.empty()) { cout << "empty file string name!" << endl; return; } else if (str_content.empty()) { cout << "empty content string!" << endl; return; } std::ofstream OsWrite(file_string, std::ofstream::app); OsWrite << str_content; OsWrite << std::endl; OsWrite.close(); } void readFromFile(string file_string) { if (file_string.empty()) { cout << "empty file string name!" << endl; return; } string line; std::ifstream labels(file_string.c_str()); if (!labels.is_open()) { cout << "maybe the file is not exist, can not open the file! " << endl; return; } while (std::getline(labels, line)) { cout << line << endl; } } string getParamStr(char *ch_pt) { string str; char *ptr = new char[MAXLEN]; ptr = ch_pt; str = ptr; cout << "parameter: " << str << endl; return str; } /* ... : 不定參數(shù) */ void ProcessByPattern(char *ptr, ...) { string pattern_str = getParamStr(ptr); va_list ap; va_start(ap, ptr); if (pattern_str == "-r") { // va_arg(ap, type): 獲取下一個(gè)type類型的參數(shù) char *para_ptr = va_arg(ap, char *); string readFileStr = getParamStr(para_ptr); readFromFile(readFileStr); } else if (pattern_str == "-w") { char *file_ptr = va_arg(ap, char *); char *cont_ptr = va_arg(ap, char *); string filename = getParamStr(file_ptr); string cont_str = getParamStr(cont_ptr); write2File(filename, cont_str); } else { cout << "pattern is empty or pattern number is wrong" << endl; } va_end(ap); } int main(int argc, char *argv[]) { // cout<<argc<<endl; // cout<<argv[0]<<endl; // cout<<argv[1]<<endl; if (argc < 2) { cout << "no arguments pass throught command line" << endl; return -1; } cout << "請(qǐng)輸入模式和參數(shù):" << endl; cout << "如: -r filename, 即從filename逐行讀取內(nèi)容并打印" << endl; cout << "-w filename content, 向filename寫入content" << endl; cout << "若寫入的文件對(duì)象不存在,則其將會(huì)被創(chuàng)建" << endl; ProcessByPattern(argv[1], argv[2], argv[3]); return 0; }
g++ mine_shell_0.1.cpp -o mine_shell_0.1
./mine_shell_0.1 -w y.log 99999999999999999999999999
./mine_shell_0.1 -r y.log
99999999999999999999999999
想讓它更像shell命令的話,三種方式:
- 軟鏈接
- bashrc中的別名
- 把它移動(dòng)到系統(tǒng)環(huán)境目錄下
到此這篇關(guān)于C++模擬Linux Shell編寫一個(gè)自定義命令的文章就介紹到這了,更多相關(guān)C++自定義命令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Qt實(shí)現(xiàn)自定義時(shí)間選擇控件
這篇文章主要為大家詳細(xì)介紹了如何基于Qt實(shí)現(xiàn)自定義時(shí)間選擇控件,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12C++使用TinyXml實(shí)現(xiàn)讀取XMl文件
常見C/C++?XML解析器有Tinyxml、XERCES、squashxml、xmlite、pugxml、libxml等等,本文為大家介紹的是使用TinyXml實(shí)現(xiàn)讀取XMl文件,需要的可以參考一下2023-06-06詳解C++標(biāo)準(zhǔn)庫(kù)中處理正則表達(dá)式的類std::regex
std?是?C++?標(biāo)準(zhǔn)庫(kù)的命名空間,包含了大量標(biāo)準(zhǔn)的?C++?類、函數(shù)和對(duì)象,這些類和函數(shù)提供了廣泛的功能,包括輸入輸出、容器、算法、字符串處理等,這篇文章主要介紹了C++標(biāo)準(zhǔn)庫(kù)中提供的用于處理正則表達(dá)式的類std::regex,需要的朋友可以參考下2024-03-03C語(yǔ)言之malloc動(dòng)態(tài)分配內(nèi)存和free釋放
這篇文章主要介紹了C語(yǔ)言之malloc動(dòng)態(tài)分配內(nèi)存和free釋放,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07VC創(chuàng)建進(jìn)程CreateProcess的方法
這篇文章主要介紹了VC創(chuàng)建進(jìn)程CreateProcess的方法,涉及VC操作進(jìn)程的基本技巧,需要的朋友可以參考下2015-05-05C++實(shí)現(xiàn)LeetCode(72.編輯距離)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(72.編輯距離),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07