I am trying to get userinput from stdin and store the lines in an array.
If i do this:
using a char **list to store strings
allocate memory to it
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *prog = argv[0];
char **linelist;
int listlen = 3;
int index = 0;
char *line;
int linelen = 80;
int len = 0;
/*allocate memory*/
/*****************/
linelist = calloc(sizeof(char **), listlen);
if (linelist == NULL) {
fprintf(stderr, "%s: Error. Could not allocate memory\n", \
prog);
exit(42);
}
line = malloc(linelen);
if (line == NULL) {
fprintf(stderr, "%s: Error. Could not allocate memory\n", \
prog);
}
/*fill it manually*/
/******************/
linelist[0] = "foo";
linelist[1] = "bar";
linelist[2] = "baz";
/*print result on stdout, dummy*/
/*******************************/
for (index = 0; index < 3; ++index) {
printf("%s\n", linelist[index]);
}
return 0;
}
it works. As far i see.
If i do this:
using a char **list
allocate memory to it
using getline to get input from stdin
allocate memory to each line:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *prog = argv[0];
char **linelist;
int listlen = 3;
int index = 0;
char *line;
int linelen = 80;
int len = 0;
/*allocate memory*/
/*****************/
linelist = calloc(sizeof(char **), listlen);
if (linelist == NULL) {
fprintf(stderr, "%s: Error. Could not allocate memory\n", \
prog);
exit(42);
}
line = malloc(linelen);
if (line == NULL) {
fprintf(stderr, "%s: Error. Could not allocate memory\n", \
prog);
}
/*read input with getline*/
/*************************/
/* the former while fails, lets check if the latter succeeds: well, kinda*/
/*while (len = getline(&line, &linelen, stdin) && index < linelen -1 ) ) {*/
while (index < listlen && (len = getline(&line, &linelen, stdin)) ) {
/* debugging purpose*/
printf("size of line is: %d\n", len);
linelist[index] = malloc(len);
if (linelist[index] == NULL) {
fprintf(stderr, "%s: Error. Could not allocate memory\n", \
prog);
}
linelist[index] = line;
/*debugging purpose*/
printf("line is: %s\n", linelist[index]);
/*gives correct output*/
++index;
}
/*result*/
/********/
/* both gives wrong output,all give the last string entered on stdin*/
for (index = 0; index < listlen -1; index++) {
printf("element %d of linelist is: %s\n", \
index, linelist[index]);
}
printf("%s\n", linelist[0]);
printf("%s\n", linelist[1]);
printf("%s\n", linelist[2]);
return 0;
}
It fails.
What seems to get stored in the **list is three times the last input i get from stdin.
I don't understand why.
Here is the way i get the stdin as a standalone:
while (index < listlen && (len = getline(&line, &linelen, stdin)) ) {
/* debugging purpose*/
printf("size of line is: %d\n", len);
linelist[index] = malloc(len);
if (linelist[index] == NULL) {
fprintf(stderr, "%s: Error. Could not allocate memory\n", \
prog);
}
linelist[index] = line;
/*debugging purpose*/
printf("line is: %s\n", linelist[index]);
/*gives correct output*/
++index;
}
printf("line is. %s\n", linelist[index]
gives the right string (the actual stdin). But it doesn't seem to get stored (correct) in "char **linelist".
thanks for a hint.