fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <inttypes.h>
  5. #include <errno.h>
  6.  
  7. void printLinePair(const char *line)
  8. {
  9. size_t size = 0;
  10. while (line[size] != ' ' && line[size] != '\0') {
  11. size++;
  12. }
  13.  
  14. if (size > 0) {
  15. fwrite(line, sizeof(char), size, stdout);
  16. }
  17.  
  18. printf(" : ");
  19.  
  20. line += size;
  21. size = 0;
  22.  
  23. while (*line == ' ' && *line != '\0') {
  24. line++;
  25. }
  26.  
  27. while (line[size] != ' ' && line[size] != '\0') {
  28. size++;
  29. }
  30.  
  31. if (size > 0) {
  32. fwrite(line, sizeof(char), size, stdout);
  33. }
  34.  
  35. printf("\n");
  36. }
  37.  
  38. #define BUFFER_SIZE 6
  39. int main()
  40. {
  41. static char buffer[BUFFER_SIZE] = { 0 };
  42. static size_t bufIdx = 0;
  43. static size_t readBytes = 0;
  44. int ret = EXIT_SUCCESS;
  45. char *lineBuffer = NULL;
  46. char *lineBufPtr = lineBuffer;
  47. size_t lineBufferSize = 0;
  48. size_t lineBufferCapacity = BUFFER_SIZE + 1;
  49. size_t newLineBufferCapacity = 0;
  50. FILE *file = stdin; // fopen("1.txt", "r"); // <========= FILE HERE
  51.  
  52. if (!file) {
  53. printf("Couldn't open file: %d\n", errno);
  54. ret = EXIT_FAILURE;
  55. goto fail_open_file;
  56. }
  57.  
  58. lineBuffer = malloc(lineBufferCapacity);
  59. if (!lineBuffer) {
  60. printf("Couldn't allocate the line buffer.");
  61. ret = EXIT_FAILURE;
  62. goto fail_allocate_line_buffer;
  63. }
  64.  
  65. while ((readBytes = fread(buffer, sizeof(char), BUFFER_SIZE, file))) {
  66. if (lineBufferCapacity - lineBufferSize < readBytes + 1) {
  67. newLineBufferCapacity = lineBufferCapacity * 2;
  68. lineBufPtr = realloc(lineBuffer, newLineBufferCapacity);
  69. if (!lineBufPtr) {
  70. printf("Couldn't realloc the line buffer (%zu to %zu bytes).\n",
  71. lineBufferCapacity, newLineBufferCapacity);
  72. ret = EXIT_FAILURE;
  73. goto fail_realloc_line_buf;
  74. } else {
  75. lineBuffer = lineBufPtr;
  76. lineBufferCapacity = newLineBufferCapacity;
  77. }
  78. }
  79.  
  80. while (bufIdx < readBytes && buffer[bufIdx] != '\0') {
  81. while (bufIdx < readBytes && buffer[bufIdx] != '\n' && buffer[bufIdx] != '\0') {
  82. lineBuffer[lineBufferSize++] = buffer[bufIdx++];
  83. }
  84.  
  85. if (bufIdx < readBytes && buffer[bufIdx] != '\0') {
  86. lineBuffer[lineBufferSize++] = '\0';
  87. printLinePair(lineBuffer);
  88. lineBufferSize = 0;
  89. lineBuffer[0] = '\0';
  90. bufIdx++;
  91. }
  92. }
  93.  
  94. bufIdx = 0;
  95. }
  96.  
  97. if (lineBufferSize > 0) {
  98. lineBuffer[lineBufferSize++] = '\0';
  99. printLinePair(lineBuffer);
  100. }
  101.  
  102. fail_realloc_line_buf:
  103. free(lineBuffer);
  104. fail_allocate_line_buffer:
  105. fclose(file);
  106. fail_open_file:
  107. return ret;
  108. }
  109.  
Success #stdin #stdout 0s 9424KB
stdin
1 2
Hello  World
Lol  Rofl
govnokod ru
stdout
1 : 2
Hello : World
Lol : Rofl
govnokod : ru