fork download
  1. #include <cstring>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cassert>
  5.  
  6. using namespace std;
  7.  
  8. typedef unsigned size_t;
  9.  
  10. char* read_all_text(const char* _unused, size_t* len ) {
  11. const char* buf = "The quick brown fox jumps\nover\n\nthe lazy dog.";
  12. size_t L = strlen(buf);
  13.  
  14. char* p = new char[L+1];
  15. strcpy(p,buf);
  16. p[L] = 0;
  17.  
  18. *len = L;
  19.  
  20. return p;
  21. }
  22.  
  23. int main() {
  24. const char* path = nullptr;
  25.  
  26. // read
  27. size_t len = 0;
  28. char* str = read_all_text(path,&len);
  29. const char* end = str + len;
  30.  
  31. // count and allocate
  32. size_t line_ct = count((const char*)str,end,'\n') + 1;
  33.  
  34. size_t ldx = 0;
  35. const char** lines = new const char*[line_ct];
  36.  
  37. // process
  38. lines[ldx++] = str;
  39. for ( char* p = str; p < end; ++p ) {
  40. switch ( *p ) {
  41. case '\n':
  42. assert(ldx < line_ct);
  43. lines[ldx++] = p + 1;
  44. case '\r':
  45. *p = 0;
  46. break;
  47.  
  48. default: ;
  49. }
  50. }
  51.  
  52. // beep boop
  53. for ( size_t idx = 0; idx < line_ct; ++idx )
  54. printf("%i %s\n",idx,lines[idx]);
  55.  
  56. // clean-up
  57. delete[] lines;
  58. delete[] str;
  59. }
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
0 The quick brown fox jumps
1 over
2 
3 the lazy dog.