The input file data size is unknown so I would have to use malloc( ) to allocate enough memory for the file data. In order to do this, I would need to read each character of the file possibly by using a counter and then returning the counter as "totalChar" and allocating memory for it. The code I have so far is:
I tried to put in a counter in the while loop but that did not work at all. It just stopped the loop somehow. I'm stumped.
My memory allocation would look like this:
Am I on the right track of putting the input file into a single string?
Thanks!
Code:
#define FILENAME_SIZE 15
#define FILESIZE_MAX 10000
char filename[FILENAME_SIZE];
char string[FILESIZE_MAX];
FILE *ifp;
printf("Enter the name of the file to analyze :");
scanf("%s", filename);
printf("\n");
ifp = fopen(filename, "r");
if (ifp == NULL)
{
fprintf(stderr, "Could not open file: '%s' ", filename);
exit (-1);
}
int c = fgetc(ifp);
while (c != EOF)
{
printf("%c", c);
c=fgetc(ifp);
}
fclose(ifp);
My memory allocation would look like this:
Code:
string = (char) malloc(totalChar * sizeof(char));
Thanks!
Comment