Reading input from a file. Program crashes
Okay, so I'll start off by telling you what I am trying to do.
I have a text file that has words on each line. E.g.
Code:
this
is
a
test
forMyProgram
I want my program to scan through the file and store each word on its own line in my variable 'fullSub'. I want to then be able to use that variable as a string. Here's what I have so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *fp;
static const char filename[] = "subdomains.txt";
char *subs;
char *fullSub = malloc(sizeof(char));
fp = fopen(filename, "r");
if (fp != NULL)
{
while((subs = fgetc(fp)) != EOF && subs != '\n' && subs != ' ')
{
realloc(fullSub, fullSub+1);
strcat(fullSub, subs);
}
}else{
fprintf(stderr, "Failed to open file '%s'!\n", filename);
return 1;
}
free(fullSub);
system("pause");// just here for testing purposes... I will use getchar() at a later date.
return 0;
}