0

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.

4
Contributors
9
Replies
14
Views
7 Years
Discussion Span
Last Post by jephthah
1

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


.

Edited by jephthah: n/a

0

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

Edited by urbangeek: n/a

0

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

.

Edited by jephthah: n/a

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.

Edited by Aia: n/a

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.

That sounds good dude......

Working nicely. Thanks a lot

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.

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.

Edited by jephthah: n/a

4

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'

Votes + Comments
Yuppie, that's what they often forget :p
bada bing
This question has already been answered. 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.