C + + file read (ii)---Simple log positioning system with source code

Source: Internet
Author: User

Simple log Positioning system----Read EXE execution file path in the specified format of the file content, according to the LOG keyword output specified results, to achieve simple problem positioning
The steps are simple:
1, clock () function, used for timing, see performance geometry
2. Dynamically get the current execution file path
3, split the ' path ' string, and then reassemble, the boost library has a ready-made split function, you can also use the stroke function to achieve their own
4, type conversion, the path to be replaced by wstring type
5. Find all files in the specified format under path
6. Read the contents of the file into the cache
7, Business logic processing, according to the LOG keyword output to specify the results
8. End

The following code is only the most primitive, business logic processing No example, the code itself is program-oriented, there are many places in order to understand, a bit repetitive, just realized its function


#include <iostream> #include <fstream> #include <string> #include <vector> #include < windows.h> #include <boost/algorithm/string/split.hpp> #include <boost/algorithm/string/ Classification.hpp>//is_any_ofusing namespace Std;int main () {clock_t tstart,tend; Tstart = Clock (); string strpath;//dynamic Execution program path vector<string> everypath;//holds each level directory string spath = "";// After assembling the string path wstring Wspath = L "";//wstring full path wstring Wsfullpath = L "";//wstring full path and file name (actually * filename) wstring Wstruefullpath = L ""; Wstring true full path and filename vector<wstring> veceveryfile; All files in the specified format under a directory vector<wstring> vecfullfile;//the absolute path of the file name that satisfies the condition vector<string> vecfilecontent;// Store each level of directory string Strfilecontent;int nfilecount = 0;char chpath[100]; GetCurrentDirectory (max_path,chpath); strpath = chpath;cout << strpath << endl;boost::split (EveryPath, Strpath,boost::is_any_of (", \ \"), boost::token_compress_on); for (Vector<string>::iterator iter= Everypath.begin (); iter!= everypath.end (); iter++) {string Stemppath = *iter;spath = spath + stemppath + "\ \" + "\ \";} cout << spath << endl;const char* chpath = Spath.c_str (); size_t ipathnum = strlen (chpath) +1; const size_t NEW Size = 100;size_t Convertedchars = 0;wchar_t wnewstring[newsize];mbstowcs_s (&convertedchars,wnewstring,ipathnum, chpath,_truncate) Wspath = wnewstring;wcout << wspath << endl;wsfullpath = Wspath + L "*.log"; wcout << ws FullPath << endl;_wfinddata_t Fileinfo;long handle = _wfindfirst (Wsfullpath.c_str (), &fileinfo); if (-1 = = Handle) {cout << nfilecount << Endl; Tend = Clock ();d ouble duration = (double) (tend-tstart) cout << "Can not find the log" << endl;cout << Duration << Endl;return 0;} Do{nfilecount ++;wstring wtempfile = Fileinfo.name;veceveryfile.push_back (wtempfile);} while (_wfindnext (handle,&fileinfo) ==0) cout << nfilecount << endl;for (vector<wstring>:: Iterator iter = Veceveryfile.begin (); ITER! = VeceVeryfile.end (); iter + +) {wstring wtempFilename1 = *iter;wstring Wfullfilename = Wspath+wtempfilename1;vecfullfile.push _back (wfullfilename);} for (Vector<wstring>::iterator iter = Vecfullfile.begin (); ITER! = Vecfullfile.end (); iter + +) {ifstream infile (* ITER); while (infile >> strfilecontent) {vecfilecontent.push_back (strfilecontent);}} This side just look at the exe path under the file content is read all to the cache, the actual words to eradicate the Log keyword output different content can be for (vector<string>::iterator iter = Vecfilecontent.begin ( ); iter = Vecfilecontent.end (); iter + +) {cout << *iter << Endl;} Tend = Clock ();d ouble duration = (double) (Tend-tstart), cout << duration << endl;system ("PAUSE"); return 0;}

In the file.exe of the road under the Alarm.log and Trap.log, the contents are as follows

File.exe execution results are as follows, successfully read the contents of the two log files into the cache, the real log positioning system also to implement the data in the cache processing, according to different values to output different values to the interface, so that people can not understand the log only need to execute the EXE file, directly see the output will be able to

C + + file read (ii)---Simple log positioning system with source code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.