fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. int main() {
  7.  
  8. // Consider asciiimage is your text file input
  9. std::istringstream asciiimage(R"input(1,434,341,158,498
  10. 5,316,211,323,269
  11. 42,508,645,232,2)input");
  12.  
  13.  
  14. std::vector<std::vector<int>> pixelrows;
  15. std::string line;
  16. while(std::getline(asciiimage,line)) {
  17. pixelrows.push_back(std::vector<int>());
  18. std::istringstream linein(line);
  19. int num;
  20. while(linein >> num || !linein.eof()) {
  21. if(linein.fail()) {
  22. linein.clear();
  23. char dummy;
  24. linein >> dummy;
  25. continue;
  26. }
  27. pixelrows.back().push_back(num);
  28. }
  29. }
  30.  
  31. for(auto itRow = pixelrows.begin();
  32. itRow != pixelrows.end();
  33. ++itRow) {
  34. for(auto itCol = itRow->begin();
  35. itCol != itRow->end();
  36. ++itCol) {
  37. if(itCol != itRow->begin()) {
  38. std::cout << ", ";
  39. }
  40. std::cout << *itCol;
  41. }
  42. std::cout << std::endl;
  43. }
  44. return 0;
  45. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
1, 434, 341, 158, 498
5, 316, 211, 323, 269
42, 508, 645, 232, 2