fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. int main ()
  6. {
  7. // 1. read each line from the file into a string with std::getline()
  8. // http://w...content-available-to-author-only...s.com/reference/string/string/getline/
  9.  
  10. // as an wexample, say, a line read from the file is:
  11. std::string line = "x1888.88 14.56 123456789 xxx yyy zzz" ;
  12.  
  13. // 2. create an input string stream which reads from the line
  14. std::istringstream stm(line) ;
  15.  
  16. // 3. read the fields from the input string stream
  17. char junk ;
  18. char type ;
  19. float hours ;
  20. double salary ;
  21. long id ;
  22. std::string fname ;
  23. std::string mname ;
  24. std::string lname ;
  25.  
  26. if( stm >> junk >> type >> hours >> salary >> id >> fname >> mname >> lname )
  27. {
  28. std::cout << "junk (to be thrown away): " << junk << '\n'
  29. << "type: " << type << '\n'
  30. << "hours: " << hours << '\n'
  31. << "salary: " << salary << '\n'
  32. << "id: " << id << '\n'
  33. << "fname: " << fname << '\n'
  34. << "mname: " << mname << '\n'
  35. << "lname: " << lname << '\n' ;
  36. // ...
  37. }
  38. else
  39. {
  40. // badly formed line
  41. }
  42. }
  43.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
junk (to be thrown away): x
type: 1
hours: 888.88
salary: 14.56
id: 123456789
fname: xxx
mname: yyy
lname: zzz