fork download
  1. #include <fstream>
  2. #include <regex>
  3. #include <string>
  4. #include <iostream>
  5. #include <cstdlib>
  6.  
  7.  
  8. int main(int argc, char **argv) {
  9. using namespace std;
  10.  
  11. if (argc <= 1) {
  12. cerr << "File name required" << endl;
  13. return -1;
  14. }
  15.  
  16. ifstream stream(argv[1], ios::in);
  17. if (!stream.is_open()) {
  18. cerr << "Could not open file " << argv[1] << endl;
  19. return -1;
  20. }
  21.  
  22. regex rx("(\\w+):(\\d+)\\$");
  23. int book_counter = 0;
  24. string line;
  25. while (getline(stream, line)) {
  26. smatch results;
  27. if (regex_search(line, results, rx)) {
  28. auto book_name = results.str(1);
  29. auto book_count = atoi(results.str(2).data());
  30. book_counter += book_count;
  31.  
  32. cout << "Entry added: " << book_name << ", count " << book_count << endl;
  33. }
  34. }
  35.  
  36. cout << "Total books: " << book_counter << endl;
  37.  
  38. return 0;
  39. }
  40.  
Runtime error #stdin #stdout #stderr 0s 15344KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
File name required