本人初學c++ 如有遺漏和錯誤,望斧正!本文只是簡單的介紹一下c++對txt檔案的簡單讀和寫
希望大家喜歡
給個贊就最好了
(。・∀・)ノ
在windows下讀取txt檔案時往往需要從中提取一些內容,比如gps模組返回日誌的txt檔案中經緯度的提取因此為了方便我們可以利用簡單的正則將其輸出$gnrmc,071550.000,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,a*7f
gps檔案內容名稱為gpstest.txt
$gnrmc,071549.000,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,,,a*77
$gnrmc,071549.200,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,,,a*75
$gnrmc,071549.400,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,,,a*73
$gnrmc,071549.600,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,,,a*71
$gnrmc,071549.800,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,,,a*7f
$gnrmc,071550.000,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,,,a*7f
$gnrmc,071550.200,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,,,a*7d
$gnrmc,071550.400,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,,,a*7b
$gnrmc,071550.600,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,,,a*79
$gnrmc,071550.800,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,,,a*77
$gnrmc,071551.000,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,,,a*7e
$gnrmc,071551.200,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,,,a*7c
$gnrmc,071551.400,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,,,a*7a
$gnrmc,071551.600,a,2325.0070,n,11637.7624,e,000.0,000.0,170920,,,a*78
#include
其中擁有正則運算相關的函式
string str = "hello_2018";
smatch result;//regex專用儲存
regex pattern("(.)_(\\d)"); //匹配5個任意單字元 + 下劃線 + 4個數字
if (regex_match(str, result, pattern))
string inf;
regex pattern1("\\d.\\d,\\w");
regex pattern2("\\d.\\d,\\w");
smatch result1;
smatch result2;
while (getline(infile, inf))//getline(input,string(content)) 之所以不用 infile.get(ch) 和 infile.getline(ch,number)是因為其儲存的是char和char陣列
}} infile.close();
system("pause");
return 0 ;
}
c++檔案的簡單讀取
結果如圖所示
正則的簡單運用
這段 是正則匹配單個大寫字母,並對字串進行操作。m.find 是找到匹配的那一項,可以理解為乙個指標,指到匹配的位置,m.group 則是得到指向的值。第乙個引數是乙個stringbuffer。第二個引數是在m.find 的位置後新增的字串。此方法的作用是將m.find 指向位置前邊的字串和arg2...
正則簡單封裝
正則 reg.js var reg ip 1 d d 2 0 4 d 25 0 5 d 1 d d 2 0 4 d 25 0 5 d 1 d d 2 0 4 d 25 0 5 d 1 d d 2 0 4 d 25 0 5 message 請輸入正確的ip位址 specialstr iphone me...
簡單的正則表達示。
驗證數字 1 驗證n位的數字 d 驗證至少n位數字 d 驗證m n位的數字 d 驗證零和非零開頭的數字 0 1 9 0 9 驗證有兩位小數的正實數 2 0 9 驗證有1 3位小數的正實數 3 0 9 驗證非零的正整數 1 9 0 9 驗證非零的負整數 1 9 0 9 驗證非負整數 正整數 0 d 驗...