Hi, again. I'm trying to read in data from a file that I've opened, so it is sitting in FILE* fp. Now I want to put it into a format that I can do things with it (seperate it into words). My question is: how can I do this dynamically? I found code (below) for putting files into a string but if I open a file of more than a certain length (in the example below, 128) then I will encounter problems, if I choose a huge fixed length then I am being very inefficient. With regards to my end goal, is there a way to either determine the length of the file then choose the string from that, or not use strings at all, maybe lists or such?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
char str[128];
if((fp = fopen(argv[ 1 ], "r"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
while(!feof(fp)) {
if(fgets(str, 126, fp))
printf("%s", str);
}
fclose(fp);
return 0;
}
Much love,
sd