Hi,
I have a problem with fread,i wrote that program
and in the input file only "Hello Word".
the out put of the program only prints "Hell"
and when I increase the elements in fread to 2 it
outputs "Hello Wo".
so for each element in fread it reads 4
characters (note i am using sizeof(Char*)
Please help..Thanks in advance.
Here is my code:

int main(void)
{
int x;
FILE *ifp;
char *text[MAX];
for(x=0;x<MAX;x++)
{
	text[x]=(char *)calloc(MAX,sizeof(char));
}
ifp=fopen("in.txt","r");
fread(text[0],sizeof(char*),1,ifp);
printf("first word is %s\n",text[0]);
fclose(ifp);
return 0;
}

Edited 6 Years Ago by Nick Evan: Replaced ICODE tags with CODE tags. CODE-tags are for blocks of code. ICODE is for 1 line.

>>ifp=fopen("in.txt","r");
That is opening the file in text mode while fread() expects binary mode. Suggest you replace fread() with fgets() if you want to keep text mode.

>>fread(text[0],sizeof(char*),1,ifp);

sizeof(char*) is always the sjze of a pointer, which on MS-Windows and *nix 32-bit compilers is 4. And that's why fread() is only reading 4 characters.

Edited 6 Years Ago by Ancient Dragon: n/a

thanks for your reply..
it helped alot..
how can i get what inside the file to a single destination?
as fgets works line by line..
thanks in advance.

This question has already been answered. Start a new discussion instead.