fork download
  1. #include <iostream>
  2. #include <sstream> // replace this with fstream
  3.  
  4. int main()
  5. {
  6. const int MAX_STRINGS = 10;
  7. const int MAX_STRING_LEN = 20;
  8.  
  9. char* strings[MAX_STRINGS] = {0};
  10.  
  11. std::istringstream ss("test1,test2,test3"); // replace this with std::ifstream
  12.  
  13. char* str;
  14. int i = 0;
  15.  
  16. while(ss)
  17. {
  18. str = new char[MAX_STRING_LEN];
  19. ss.getline(str, MAX_STRING_LEN, ',');
  20. strings[i++] = str;
  21. }
  22.  
  23. // print each string then delete it
  24. for(int i = 0; i < MAX_STRINGS; ++i)
  25. {
  26. if(strings[i])
  27. {
  28. std::cout << strings[i] << std::endl;
  29. delete[] strings[i];
  30. }
  31. }
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
test1
test2
test3