Here is my code so far. My array just isn't being populated. The problem is happening with fscanf. I know it reads in the characters just fine because I've tried reading them into a generic char pointer / string and it reads just fine. Just not sure how to copy the chars into the complicated array. Pointer to a pointer :O Text file format might something like..
Alice
Bob
Jerry
Ted
My code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int lineCount = 1;
char* fileInput;
char c[100];
int allocatedBytes = 0;
fileInput = (char*)malloc(100*sizeof(char));
printf("File name: ");
gets(fileInput);
FILE * fptr;
if ((fptr = fopen(fileInput, "r")) == NULL)
{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);
}
//get number of names before dynamic array allocation
for(char c = getc(fptr); c!= EOF; c=getc(fptr))
if(c == '\n')
lineCount = lineCount + 1;
char **names = malloc(lineCount * sizeof(char *));
for(int i=0; i<lineCount; i++)
names[i] = (char *)malloc(100);
int i=0;
while((fscanf(fptr, "%99s", names[i]))!=EOF)
i++;
for(int i=0; i<lineCount; i++)
printf("%s\n", names[i]);
}