fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. size_t dynamic_reader(FILE *file, char ***strings);
  5.  
  6. int main(void)
  7. {
  8. char **strings;
  9. size_t lines = dynamic_reader(stdin, &strings);
  10.  
  11. for (size_t i = 0; i < lines; ++i)
  12. {
  13. printf("Read input: \"%s\"\n", strings[i]);
  14. }
  15.  
  16. // Free all the dynamically allocated memory
  17. for (size_t i = 0; i < lines; ++i)
  18. {
  19. // Free a string
  20. free(strings[i]);
  21. }
  22. // Free the array
  23. free(strings);
  24. }
  25.  
  26. // Function:
  27. // read_line - Reads a single line from a file
  28. // Argument:
  29. // file - the file to read the input from
  30. // Returns:
  31. // The line read
  32. char *read_line(FILE *file)
  33. {
  34. int ch;
  35. char *s = NULL;
  36. size_t current_length = 0; // Current length of string
  37.  
  38. // Loop to read a single line
  39. while ((ch = fgetc(file)) != EOF)
  40. {
  41. if (ch == '\n')
  42. break; // Newline, done with the current string
  43.  
  44. if (s == NULL)
  45. {
  46. s = malloc(2); // Allocate two character: One for c and one for the terminator
  47. }
  48. else
  49. {
  50. // The current length is not including the terminator
  51. // that's why we add two characters
  52. char *temp_s = realloc(s, current_length + 2);
  53. if (temp_s == NULL)
  54. {
  55. // Handle error
  56. }
  57. s = temp_s;
  58. }
  59.  
  60. s[current_length++] = ch;
  61. s[current_length] = '\0'; // Terminate as a string
  62. }
  63.  
  64. return s;
  65. }
  66.  
  67. // Function:
  68. // dynamic_reader - Reads multiple lines from a file
  69. // Argument:
  70. // file - the file to read the input from
  71. // strings - a pointer to the array of strings where the lines would be put
  72. // Returns:
  73. // The number of strings in the array
  74. size_t dynamic_reader(FILE *file, char ***strings)
  75. {
  76. size_t current_line = 0;
  77. *strings = NULL;
  78.  
  79. // Loop until we hit the end of the file, or an error
  80. do
  81. {
  82. char *s = read_line(file);
  83. if (s != NULL)
  84. {
  85. // "Add" the string to the array of strings
  86. if (*strings == NULL)
  87. {
  88. // Initial allocation
  89. *strings = malloc(1 * sizeof(**strings));
  90. }
  91. else
  92. {
  93. // Reallocate array to increase size by one
  94. char **temp_strings = realloc(*strings, (current_line + 1) * sizeof(**strings));
  95. if (temp_strings == NULL)
  96. {
  97. // Handle error
  98. }
  99. *strings = temp_strings;
  100. }
  101. (*strings)[current_line++] = s;
  102. }
  103.  
  104. } while (!feof(file) && !ferror(file));
  105.  
  106. return current_line;
  107. }
  108.  
Success #stdin #stdout 0s 2248KB
stdin
hello
world
foo bar
this is a longer string than the others just to show off
x
blaha blaha
stdout
Read input: "hello"
Read input: "world"
Read input: "foo bar"
Read input: "this is a longer string than the others just to show off"
Read input: "x"
Read input: "blaha blaha"