fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Ref: http://w...content-available-to-author-only...s.com/reference/iterator/istreambuf_iterator/
  5. // Ref: http://w...content-available-to-author-only...s.com/reference/istream/istream/sentry/
  6. // Ref: http://w...content-available-to-author-only...s.com/reference/istream/istream/
  7.  
  8. char *buffer_realloc(char *buf, size_t oldSize, size_t newSize)
  9. {
  10. if (oldSize == newSize)
  11. {
  12. return buf;
  13. }
  14.  
  15. char *newBuf = nullptr;
  16. size_t count = 0;
  17.  
  18. if (oldSize > newSize)
  19. count = newSize;
  20. else
  21. count = oldSize;
  22.  
  23. newBuf = new char[newSize];
  24. std::copy(buf, buf + count, newBuf);
  25.  
  26. delete[] buf;
  27. return newBuf;
  28. }
  29.  
  30. istream& getline(istream& is, char *&buffer)
  31. {
  32. std::istream::sentry s(is);
  33. if (s)
  34. {
  35. std::istreambuf_iterator<char> it(is);
  36. std::istreambuf_iterator<char> end;
  37.  
  38. size_t size = 4;
  39. size_t grow = 4;
  40. size_t len = 0;
  41.  
  42. buffer = new char[size];
  43.  
  44. while (it != end && *it != '\n')
  45. {
  46. if (len == (size - 1))
  47. {
  48. buffer = buffer_realloc(buffer, size, size + grow);
  49. size += grow;
  50. }
  51.  
  52. buffer[len++] = *it++;
  53. }
  54.  
  55. buffer[len] = '\0';
  56. }
  57.  
  58. return is;
  59. }
  60.  
  61. int main()
  62. {
  63. char *line = nullptr;
  64. while (getline(std::cin, line))
  65. {
  66. if (line)
  67. {
  68. cout << line << endl;
  69. delete[] line;
  70. }
  71. }
  72.  
  73. return 0;
  74. }
Success #stdin #stdout 0s 3460KB
stdin
line1 line 1 line1 line 1 1 1 1 111
line 2    line 2 222 2 2 222 
line 3 3 3 33 3
line 4 4 4 44444 4
stdout
line1 line 1 line1 line 1 1 1 1 111
line 2    line 2 222 2 2 222 
line 3 3 3 33 3
line 4 4 4 44444 4