fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. // a stream operator for vector<int> ouput
  8. std::ostream& operator<<(std::ostream &out, const std::vector<int> &values)
  9. {
  10. const char *sep = "";
  11. for (int value : values) {
  12. out << sep << value; sep = " ";
  13. }
  14. return out;
  15. }
  16.  
  17. void readFromFile(std::istream &in)
  18. {
  19. if (!in.good()) {
  20. std::cerr << "ERROR!" << std::endl;
  21. }
  22. std::string buffer;
  23. std::vector<int> quality;
  24. while (std::getline(in, buffer)) {
  25. if (buffer.size() >= 2 && buffer.compare(0, 2, "ID") == 0) {
  26. std::cout << buffer << std::endl;
  27. quality.clear(); // reset quality vector
  28. } else {
  29. // read numbers
  30. std::istringstream in(buffer); int qual;
  31. while (in >> qual) {
  32. quality.push_back(qual);
  33. std::cout << quality << std::endl;
  34. }
  35. }
  36. }
  37. }
  38.  
  39. int main(void)
  40. {
  41. #if 0 // in OP
  42. { std::ifstream fIn("s2.txt");
  43. readFromFile(fIn);
  44. } // fIn goes out of scope -> file is closed
  45. #else // instead
  46. readFromFile(std::cin);
  47. #endif // 0
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 16072KB
stdin
ID 1 (string) 
22 30 30 4 2 4 5 7 5 3
22 30 30 4 2 4 5 7 5 3
ID 2
30 4 2 1 2
stdout
ID 1 (string) 
22
22 30
22 30 30
22 30 30 4
22 30 30 4 2
22 30 30 4 2 4
22 30 30 4 2 4 5
22 30 30 4 2 4 5 7
22 30 30 4 2 4 5 7 5
22 30 30 4 2 4 5 7 5 3
22 30 30 4 2 4 5 7 5 3 22
22 30 30 4 2 4 5 7 5 3 22 30
22 30 30 4 2 4 5 7 5 3 22 30 30
22 30 30 4 2 4 5 7 5 3 22 30 30 4
22 30 30 4 2 4 5 7 5 3 22 30 30 4 2
22 30 30 4 2 4 5 7 5 3 22 30 30 4 2 4
22 30 30 4 2 4 5 7 5 3 22 30 30 4 2 4 5
22 30 30 4 2 4 5 7 5 3 22 30 30 4 2 4 5 7
22 30 30 4 2 4 5 7 5 3 22 30 30 4 2 4 5 7 5
22 30 30 4 2 4 5 7 5 3 22 30 30 4 2 4 5 7 5 3
ID 2
30
30 4
30 4 2
30 4 2 1
30 4 2 1 2