0

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

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by ronnieaka
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.