fork download
  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4.  
  5. struct REQUEST
  6. {
  7. unsigned int asu;
  8. unsigned int lba;
  9. long size;
  10. char opcode;
  11. double timestamp;
  12.  
  13. REQUEST()
  14. {
  15. asu = 0;
  16. lba = 0;
  17. size = 0;
  18. opcode = ' ';
  19. timestamp = 0;
  20. }
  21. };
  22.  
  23. istream& getrecord(istream& s, REQUEST& req)
  24. {
  25. string part;
  26. bool result =
  27. getline(s, part, ',') &&
  28. (istringstream(part) >> req.asu) &&
  29. getline(s, part, ',') &&
  30. (istringstream(part) >> req.lba) &&
  31. getline(s, part, ',') &&
  32. (istringstream(part) >> req.size) &&
  33. getline(s, part, ',') &&
  34. (part.size() == 1) ? (req.opcode = part[0], true) : false &&
  35. getline(s, part, ',') &&
  36. (istringstream(part) >> req.timestamp);
  37. if (!result && s)
  38. s.setstate(ios::failbit);
  39. return s;
  40. }
  41.  
  42. int main()
  43. {
  44. string original("0,303567,3584,w,0.000000");
  45.  
  46. REQUEST req;
  47. istringstream origs(original);
  48. if (getrecord(origs, req))
  49. {
  50. cout << "asu = " << req.asu <<
  51. ", lba = " << req.lba <<
  52. ", size = " << req.size <<
  53. ", opcode = " << req.opcode <<
  54. ", timestamp = " << req.timestamp;
  55. }
  56. else
  57. {
  58. cout << "FAILED";
  59. }
  60.  
  61. return 0;
  62. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
asu = 0, lba = 303567, size = 3584, opcode = w, timestamp = 0