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;
}

Recommended Answers

All 2 Replies

>>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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.