G
gordy
Newbie question, please by gentle.
I'm trying to read in a text file containing the days of the week, one on
each line. Each line should be pointed to by an array of pointers to char
but I'm ending up with each pointer pointing to the same string.
#include <stdio.h>
#include <stdlib.h>
#define DAYS 7
int main()
{
int count = 0, bytes_read;
char *strings[DAYS], *buffer = NULL;
FILE *fp;
size_t size = 1;
if((fp = fopen("days", "r")) == NULL)
{
puts("File not found");
exit(EXIT_FAILURE);
}
while(bytes_read = (getline(&buffer ,&size, fp)) != -1)
strings[count++] = buffer;
while(count--)
printf("\t\t%s", *(strings + count));
fclose(fp);
return 0;
}
file days:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
I'm sure I'm missing something silly here, any help would be appreciated.
gordy.
I'm trying to read in a text file containing the days of the week, one on
each line. Each line should be pointed to by an array of pointers to char
but I'm ending up with each pointer pointing to the same string.
#include <stdio.h>
#include <stdlib.h>
#define DAYS 7
int main()
{
int count = 0, bytes_read;
char *strings[DAYS], *buffer = NULL;
FILE *fp;
size_t size = 1;
if((fp = fopen("days", "r")) == NULL)
{
puts("File not found");
exit(EXIT_FAILURE);
}
while(bytes_read = (getline(&buffer ,&size, fp)) != -1)
strings[count++] = buffer;
while(count--)
printf("\t\t%s", *(strings + count));
fclose(fp);
return 0;
}
file days:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
I'm sure I'm missing something silly here, any help would be appreciated.
gordy.