C++實現(xiàn)文件逐行讀取與字符匹配的示例詳解
技術(shù)背景
用慣了python,對其他語言就比較的生疏。但是python很多時候在性能上比較受局限,這里嘗試通過C++來實現(xiàn)一個文件IO的功能,看看是否能夠比python的表現(xiàn)更好一些。關(guān)于python的類似功能的實現(xiàn),可以參考這一篇博客。
C++讀取文件
首先我們構(gòu)造一個txt文件用于測試,比如以下這個名為mindspore.txt的文件(之所以取這個名字,是因為最近在研究mindspore,因此最方便拿到的數(shù)據(jù)就是mindspore的借口api文檔):
MindSpore Python API
MindSpore Python API
mindspore
mindspore.common.initializer
mindspore.communication
mindspore.compression
mindspore.context
mindspore.dataset
mindspore.dataset.config
mindspore.dataset.text
mindspore.dataset.transforms
mindspore.dataset.vision
mindspore.explainer
mindspore.mindrecord
mindspore.nn
mindspore.numpy
mindspore.nn.probability
mindspore.ops
mindspore.profiler
mindspore.train
MindArmour Python API
mindarmour
mindarmour.adv_robustness.attacks
mindarmour.adv_robustness.defenses
mindarmour.adv_robustness.detectors
mindarmour.adv_robustness.evaluations
mindarmour.fuzz_testing
mindarmour.privacy.diff_privacy
mindarmour.privacy.evaluation
mindarmour.privacy.sup_privacy
mindarmour.utils
MindSpore Hub Python API
mindspore_hub
MindSpore Serving Python API
mindspore_serving
MindQuantum Python API
mindquantum
然后構(gòu)造一個C++代碼用于逐行讀取這個文件,通過getline函數(shù),將獲取到的行字符串保存到strline中,并且每次讀取一行都在屏幕上輸出出來。由于這里使用的是while循環(huán),因此采用index的方案設(shè)置了一個跳出循環(huán)的條件,只讀取特定的行范圍:
// iofile.cpp #include <iostream> #include <fstream> #include <string> int main() { using namespace std; string filename="mindspore.txt"; ifstream fin(filename.c_str()); int index = 0; string strline; while (getline(fin, strline) && index < 20) { cout << strline << endl; index ++; } fin.close(); cout << "Done!\n"; return 0; }
在讀取完畢后,記得使用close()將文件關(guān)閉。上述代碼的執(zhí)行結(jié)果如下:
dechin@ubuntu2004:~/projects/gitlab/dechin/$ g++ iofile.cpp
dechin@ubuntu2004:~/projects/gitlab/dechin/$ ./a.out
MindSpore Python API
MindSpore Python API
mindspore
mindspore.common.initializer
mindspore.communication
mindspore.compression
mindspore.context
mindspore.dataset
mindspore.dataset.config
mindspore.dataset.text
mindspore.dataset.transforms
mindspore.dataset.vision
mindspore.explainer
mindspore.mindrecord
mindspore.nn
mindspore.numpy
mindspore.nn.probability
mindspore.ops
mindspore.profiler
mindspore.train
Done!
這里我們使用的g++版本為9.3.0:
dechin@ubuntu2004:~/projects/gitlab/dechin/$ g++ --version
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
因為上述案例讀取的是前20行的內(nèi)容,那么在Linux下我們還可以通過head來查看前20行的文件內(nèi)容:
dechin@ubuntu2004:~/projects/gitlab/dechin/$ head -n 20 mindspore.txt
MindSpore Python API
MindSpore Python API
mindspore
mindspore.common.initializer
mindspore.communication
mindspore.compression
mindspore.context
mindspore.dataset
mindspore.dataset.config
mindspore.dataset.text
mindspore.dataset.transforms
mindspore.dataset.vision
mindspore.explainer
mindspore.mindrecord
mindspore.nn
mindspore.numpy
mindspore.nn.probability
mindspore.ops
mindspore.profiler
mindspore.train
經(jīng)過對比發(fā)現(xiàn)兩個結(jié)果是一致的。
C++字符串匹配
我們假象一個這樣的測試案例,在上述的txt文本中,我們想把帶有字符context的那一行標(biāo)記出來,使其跟其他的行不一樣。這時候就需要使用到C++的字符串匹配功能,其格式為string.find("context"),返回的是一個識別碼,用于標(biāo)記是否存在或者是存在的位置,如果字符不存在,則返回結(jié)果等價于string::npos。按照這個思路,我們定義一個布爾值,在檢索過程中如果遇到context字符就輸出1,否則輸出0,具體的代碼實現(xiàn)如下:
// iofile.cpp #include <iostream> #include <fstream> #include <string> int main() { using namespace std; string filename="mindspore.txt"; ifstream fin(filename.c_str()); int index = 0; string strline; while (getline(fin, strline) && index < 20) { bool exists = strline.find("context") == string::npos; cout << strline << '\t' << !exists << endl; index ++; } fin.close(); cout << "Done!\n"; return 0; }
上述代碼的執(zhí)行結(jié)果如下所示:
dechin@ubuntu2004:~/projects/gitlab/dechin/$ g++ iofile.cpp && ./a.out
MindSpore Python API 0
MindSpore Python API 0
mindspore 0
mindspore.common.initializer 0
mindspore.communication 0
mindspore.compression 0
mindspore.context 1
mindspore.dataset 0
mindspore.dataset.config 0
mindspore.dataset.text 0
mindspore.dataset.transforms 0
mindspore.dataset.vision 0
mindspore.explainer 0
mindspore.mindrecord 0
mindspore.nn 0
mindspore.numpy 0
mindspore.nn.probability 0
mindspore.ops 0
mindspore.profiler 0
mindspore.train 0
Done!
我們可以注意到,在含有context的那一行的行末輸出了一個1,其他行的行末輸出的都是0.
C++運行時間統(tǒng)計
在python中我們常用的一個功能是導(dǎo)入time.time()來記錄時間,然后計算兩次時間之間的差值,就可以得到一個程序的精確運行時間。C++中有一個比較類似的用法是clock_t,這里為了方便測試,我們把上述用到的代碼封裝到一個reader函數(shù)內(nèi),然后在main函數(shù)中調(diào)用以及統(tǒng)計運行時間:
// iofile.cpp #include <iostream> #include <fstream> #include <string> #include <time.h> using namespace std; int reader() { string filename="mindspore.txt"; ifstream fin(filename.c_str()); int index = 0; string strline; while (getline(fin, strline) && index < 20) { bool exists = strline.find("context") == string::npos; cout << strline << '\t' << !exists << endl; index ++; } fin.close(); cout << "Done!\n"; return 0; } int main() { clock_t start, end; start = clock(); reader(); end = clock(); cout << "The time cost is: " << double(end-start)/CLOCKS_PER_SEC << "s" << endl; }
上述代碼的執(zhí)行結(jié)果如下所示:
dechin@ubuntu2004:~/projects/gitlab/dechin/$ g++ iofile.cpp && ./a.out
MindSpore Python API 0
MindSpore Python API 0
mindspore 0
mindspore.common.initializer 0
mindspore.communication 0
mindspore.compression 0
mindspore.context 1
mindspore.dataset 0
mindspore.dataset.config 0
mindspore.dataset.text 0
mindspore.dataset.transforms 0
mindspore.dataset.vision 0
mindspore.explainer 0
mindspore.mindrecord 0
mindspore.nn 0
mindspore.numpy 0
mindspore.nn.probability 0
mindspore.ops 0
mindspore.profiler 0
mindspore.train 0
Done!
The time cost is: 0.000245s
輸出的時間表示這個函數(shù)運行時間共計0.2ms。
總結(jié)概要
本文簡單的介紹了C++中的三種基礎(chǔ)操作:逐行讀取文件內(nèi)容、字符串匹配以及運行時間的統(tǒng)計,并且通過一個簡單的范例來實現(xiàn)了這三種基本的功能。相比于python而言,C++的代碼編寫量肯定要多一些,但是考慮到C++可能帶來的效率增益,我們也應(yīng)當(dāng)了解其基本的用法以及功能實現(xiàn)。
以上就是C++實現(xiàn)文件逐行讀取與字符匹配的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于C++文件逐行讀取與字符匹配的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
undefined reference to `SetPduPowerConsumptionCnt''錯誤的解決方法
編譯時出現(xiàn)undefined reference to `SetPduPowerConsumptionCnt'錯誤要如何解決呢?有沒有什么好的解決方法?下面小編就為大家解答吧,如果你也遇到了這種情況,可以過來參考下2013-07-07C++實現(xiàn)LeetCode(65.驗證數(shù)字)
這篇文章主要介紹了C++實現(xiàn)LeetCode(65.驗證數(shù)字),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++中的std::format?如何實現(xiàn)編譯期格式檢查
C++?20?的?std::format?是一個很神奇、很實用的工具,最神奇的地方在于它能在編譯期檢查字符串的格式是否正確,而且不需要什么特殊的使用方法,只需要像使用普通函數(shù)那樣傳參即可,這篇文章主要介紹了std::format?如何實現(xiàn)編譯期格式檢查,需要的朋友可以參考下2024-04-04