Over this weekend I've been attempting to familiarize myself with regex with the intention of implementing it into a C++ program in order to extract 6 specific time-stamps, and not only output them to the console window, but output them to a simple output text file. Below is what I have mustered up so far. However, what I have below doesn't print anything to the console window, nor does it output anything to the designated output file. Could someone help me find where I've gone wrong? the regular expression works, after testing it out on http://regexr.com/
XML file: http://pastebin.com/5hMy9RzK
Screenshot of 6 time-stamps I am attempting to extract: https://i.imgur.com/G06NVlh.jpg
#include <fstream> #include <string> #include <iostream> #include <regex> using namespace std; int main() { ifstream infile; ofstream outFile; string fileinput; int i = 0; outFile.open("Outputdata.txt"); infile.open("Groupproject.xml"); // Opens the XML file containing the information that is to be read regex time_regex("\d\d\d\d-\d\d-\d\d\w\d\d:\d\d:\d\d\.716Z"); smatch time_matches; if (infile.fail()) { cout << "The file is not able to be located" << endl; system("Pause"); exit(1); } while (!infile.eof()) { //Until the end of the file is reached, obtain each line getline(infile, fileinput); if (regex_search(fileinput, time_matches, time_regex)) { // if regex_search is able to locate a line which has elements matching the regex expression "time_regex" output the located element cout << "Timestamp: " << time_matches[i] << endl; outFile << time_matches[i]; i++; } } infile.close(); outFile.close(); system("pause"); return 0; }
No comments:
Post a Comment