Mam problem ze strumieniem wejściowym, otóż drugie wykorzystanie strumienia "stdin" nic nie wypluwa. W związku z tym funkcja fgetc po odczytaniu znaku chyba kasuje lub usuwa cyklicznie dane z bufora/strumienia. Problem polega na tym, że chce policzyć ile w strumieniu wejściowym jest wierszy i później zaalokować pamięć na wskaźnik do wskaźników aby zapisać tam poszczególne linie wykorzystując funkcję getline(). Tak więc muszę 2X "przelecieć" strumień ;)
Podaje kod:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char* argv[]) {
size_t total = 0;
size_t sizeline = 0;
char** lines = NULL;
int f = 0;
int i = 0;
int n = 0;
char c = 0;
while(!feof(stdin)) {
c = fgetc(stdin);
if(c == '\n')
++i;
}
fseek(stdin, 0, 0);
// rewind(stdin);
printf("Allocated memory for pointer to pointers: %i\n", i);
printf("Size of pointer: %lu\n", sizeof(char*));
lines = (char**)malloc(i * sizeof(char*));
i = 0;
while(!(f = feof(stdin))) {
sizeline = getline(lines + i, &total, stdin);
printf("number: %i | sizeline: %lu | total: %lu | value: %s\n", i, sizeline, total, *(lines+i));
// remove end of line
lines[i][strlen(lines[i]) - 1] = 0;
if(strcmp("!quit", *(lines + i)) == 0)
break;
++i;
}
while(n < i) {
printf("%s\n", *(lines+n));
if(*(lines+n) != NULL)
free(*(lines+n));
++n;
}
free(lines);
return 0;
}