I need to get the size of a string to pass to the function read() below is the code that I have tried to implement finding the size of the string using the function size of but it doesn't work
#include <stdlib.h>
int main()
{
int num;
char *string;
FILE *fptr;
// use appropriate location if you are using MacOS or Linux
fptr = fopen("program4.txt","r");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
fread(string, /*issuse */sizeof(string), 1, fptr);
printf("%s\n", string);
fclose(fptr);
return 0;
}
fread(string, 1, n, fptr);where1is the size in bytes of achar(guaranteed to be 1) andnis the allocated size of your character array. However,freadreads raw binary and doesn't know about things such as null termination. It reads exactly1 * nbytes. So it isn't suitable unless the strings in the text file are of fixed size and zero-padded... which is probably not the case here(?).fgetsis likely more suitable since it stops reading upon end of line'\n', so in case you have a text file with strings of variable length, each written on a line of its own, thenfgetsis the way to go.freadreturns 0 upon unsuccessful read,fgetsreturns a null pointer.stringso you can't store anything in it.sizeofcan't be used on pointers, only on arrays allocated with fixed size.