I was writing a file copying program. Mind you this is an any file copier. That is, at this moment, this function can copy only text files but i'm trying to copy mp3 files, avi files, mkv files etc, which this cannot. Here is just the copying function. However it was not working...

int copy(char * source, char * target)
{
	FILE * f_source;
	FILE * f_target;
	char ch = 0;

	f_source = fopen(source,"rb");  //  OPENING IN BINARY MODE(READ)

	if(f_source == NULL)
	{
	    return 0;                   //  FILE NOT FOUND!
	}

	f_target = fopen(target,"wb");  //  OPENING IN BINARY MODE(WRITE)

	while(ch != EOF)
	{
	    ch = getc(f_source);
	    if(ch != EOF)
            putc(ch,f_target);
	}

	fclose(f_source);
	fclose(f_target);

	return 1;
}

So in desperation, i changed the type of ch in LINE 5 to int and then the program was running fine. I want to know why?
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.