Hi,
I'm a newbie............ Sorry, if I violate any norms.

Is it possible to copy any audio file or image file using C language? I think it is possible. If anyone knows it please guide me.
If you know the code, that 'll be helpful.

Recommended Answers

All 9 Replies

you can copy any file, whatever the type or extension.

just open the source file as a binary file and use a while loop to copy each byte one at a time to the destination, until the EOF charater is found.

see fopen(), fgetc(), fputc(), and feof() in the standard C library <stdio.h>. that's all the functions you'll need, though you'll probably want to include error checking with ferror().


.

just open the source file as a binary file and use a while loop to copy each byte one at a time to the destination, until the EOF charater is found.

copying byte by bye using loop...uh...sounds like a complected process...:|

copying byte by bye using loop...uh...sounds like a complected process...:|

uh... not so much :)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
   FILE *fp_source, *fp_dest;
   char oneByte;

   /* WARNING:  this does ZERO error checking!! */

   fp_source = fopen(argv[1], "rb"));
   fp_dest   = fopen(argv[2], "wb"));

   while(!feof(fp_source)) {
      oneByte = fgetc(fp_source);
      fputc(oneByte, fp_dest);
   }

   fclose(fp_source);
   fclose(fp_dest);

   return 0;
}

.

uh... not so much :)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
   FILE *fp_source, *fp_dest;
   char oneByte;

   /* WARNING:  this does ZERO error checking!! */

   fp_source = fopen(argv[1], "rb"));
   fp_dest   = fopen(argv[2], "wb"));

   while(!feof(fp_source)) {
      oneByte = fgetc(fp_source);
      fputc(oneByte, fp_dest);
   }

   fclose(fp_source);
   fclose(fp_dest);

   return 0;
}

.

How about stopping the loop without the !eof() ? eof() is not a good solution as the exit control of a loop. EOF would be true only after it is read and not when it is reached. char oneByte; must be an int, since fgetc() returns an int, and it is not a guarantee that char would be signed.

geez...i should read pointers and file data handling before understanding it properly.

How about stopping the loop without the !eof() ? eof() is not a good solution as the exit control of a loop. EOF would be true only after it is read and not when it is reached. char oneByte; must be an int, since fgetc() returns an int, and it is not a guarantee that char would be signed.

That sounds good dude......

Working nicely. Thanks a lot

How about stopping the loop without the !eof() ? eof() is not a good solution as the exit control of a loop. EOF would be true only after it is read and not when it is reached. char oneByte; must be an int, since fgetc() returns an int, and it is not a guarantee that char would be signed.

you're right about the byte should be type int, not char. it may possibly fail for some compilers. i don't know why i keep doing that, i know better.

however, i disagree with you about the eof() .. i think that's a valid way to exit the loop. unless you can remind me why it's not.

however, i disagree with you about the eof() .. i think that's a valid way to exit the loop. unless you can remind me why it's not.

A `reminder'

commented: bada bing +7
commented: Yuppie, that's what they often forget :p +8

okay, thanks :$

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.