0

I'm trying to store every line separately in an array of strings. It doesn't work the way I've written it obviously all I'm getting when I try to print array[0] is the last line of the textfile. But when I print the variable "line" inside of the while loop I can print every single line in the text file but I can only seem to store the last line of the txt file.

Is this possible to do? Or is this a limitation of getline..?

int main()
{
  FILE * fp;
  char *line;
  ssize_t read;
  size_t bufsize = 32;
  int i=0;

  char **array;

  array = malloc(bufsize * sizeof(char));
  line = malloc(bufsize * sizeof(char)); 
  fp = fopen("testing.txt", "r"); 

  while ((getline(&line, &bufsize, fp)) != -1) { 
    printf("%s", line);
    array[i] = line;
    i++;
  } 
  fclose(fp);  
  printf("%s", array[0]);

  return 0;
}
jcaron
  • 17,302
  • 6
  • 32
  • 46
felle
  • 11
  • 1
  • 4
  • 1
    "every line **separately** in an array of strings". And what makes you think a **single** `malloc` for a **single** line will make that **seperate** storage for **each** line? – too honest for this site Dec 15 '15 at 17:52
  • `array` is an array of *pointers* to `char`. So you want `array = malloc(bufsize * sizeof(char*));` (not `sizeof(char)`). Your `malloc` doesn't allocate enough memory for `bufsize` pointers. – lurker Dec 15 '15 at 17:52
  • `getline` is not standard. Use `fgets`. And `EOF` is not necessarily `-1`. Do not use magic numbers in your code! – too honest for this site Dec 15 '15 at 17:53
  • One of the convenient things about `getline()` is that it can allocate the strings for you... – Dmitri Dec 15 '15 at 17:57
  • Thanks for the answers, I'm a beginner, my teacher advised us to use getline because "it can handle most larger lines better". I will try both! – felle Dec 15 '15 at 17:58
  • @felle: He might be a typical teacher which only know his toolchain&libraries, but has no idea there is an international C standard (C11) and his toolchain is not the centre of the world. – too honest for this site Dec 15 '15 at 18:13

3 Answers3

1

As already has been mentioned, you need to allocate space for pointers, not chars:

char **array;
array = malloc(bufsize * sizeof(char*));

Also you need to allocate space for separate lines, and copy lines to it:

while ((getline(&line, &bufsize, fp)) != -1) {
  printf("%s", line);
  array[i] = malloc(strlen(line) + 1);
  strcpy(array[i], line);
  i++;
} 

A mistake in your code is that all array[i] points to the same string in variable line, which is refilled by getline() every loop cycle.

It may be useful to read manual about getline. It permits allocation of strings by itself, but don't forget to use free().

array[0] = NULL;
while ((getline(&array[i], &bufsize, fp)) != -1) {
  printf("%s", array[i]);
  array[i + 1] = NULL;
  i++;
} 
ainoss
  • 195
  • 1
  • 1
  • 9
  • Thanks a lot for the helpful advice and taking your time. Manual gave some clarity aswell. – felle Dec 15 '15 at 18:55
0

This does not make an array of strings:

char **array;
array = malloc(bufsize * sizeof(char));

There are plenty of resources that describe how to do this. eg. How to create a dynamic array of strings. Essentially, you need to allocate space for the array, then allocate space for each the strings, a standard way might be:

char **arrayOfStrings;
unsigned int numberOfElements = 20, sizeOfEachString = 100;
arrayOfStrings = malloc(numberOfElements * sizeof(char*));
for (int i = 0; i < numberOfElements ; i++)
    arrayOfStrings[i] = malloc(sizeOfEachString  * sizeof(char));

As a further note, for your whileloop:

while (getline(&line, &bufsize, fp)) {

Is sufficient. Getline will return 0 once you have finished your file. Read this.

Community
  • 1
  • 1
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
0
char **array;



array = malloc(bufsize * sizeof(char));

array[i] = line;

I think your problem is with array, you are indeed dynamically allocating memory for array but not array[i].

while ((getline(&line, &bufsize, fp)) != -1) { 
array[i]=malloc(sizeof(char) * (bufsize+1));
printf("%s", line);
array[i] = line;
i++;

}

This should fix it

Edit after testing the program:

array = malloc(bufsize * sizeof(char));

should be

array = malloc(bufsize * sizeof(char*));
Alexie Dariciuc
  • 341
  • 1
  • 3
  • 8