gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > filecopy

#151615 - darkchild - Sat Mar 01, 2008 3:37 pm

can someone whip me up a file copying function?

I can't seem to get mine working :(

here's the code if you are interested:

Code:
int copyfile(char input[], char output[])
{
   bool error;
   FILE *from, *to;
   char ch;
   
   if((from = fopen(input, "rb"))==NULL) {
    error = true;}
   
   
   if (error == false)
   {
      if((to = fopen(output, "wb"))==NULL)
      {
         error = true;
      }
    }

   if (error == false)
   {
      while(!feof(from))
      {
         ch = fgetc(from);
         if(ferror(from))
         {
            error = true;
         }
      }
      if (error == false)
      {
         if(!feof(from)){ fputc(ch, to); }
      }
      if(ferror(to))
      {
         error = true;
      }
      
      fclose(from);
      fclose(to);
      
   }
   if (error == true) { return -1;} else { return 0; }
}



If you help, your name will be on the credits of my program (iFile)

#151616 - Michoko - Sat Mar 01, 2008 3:57 pm

Well, from the top of my head, you must put the writing of your bytes in the while loop.

So you have to put:

Code:

      if (error == false)
      {
         if(!feof(from)){ fputc(ch, to); }
      }
      if(ferror(to))
      {
         error = true;
      }


inside the while loop (currently it's outside).

And you have to add to your while condition:
Code:
while(!feof(from) && (error == false))


Bye
Michoko

#151617 - darkchild - Sat Mar 01, 2008 4:26 pm

thank you greatly for the help, but I am afraid I have bad news, it still doesn't work :(

#151618 - Michoko - Sat Mar 01, 2008 4:39 pm

Hmm, difficult to figure out what's wrong with just "it doesn't work". :P
It would be good to tell us how you call this function, and maybe use some debugging messages. Is the source file opening correctly?

Anyway you should initialize your "error" variable to false, since right now it could have any initial value :

Code:
bool error = false;

#151619 - spinal_cord - Sat Mar 01, 2008 4:44 pm

Without error checking, this works...

Code:

void copy_file(char *input, char *output)
{
   FILE *from, *to;
   char ch;
   from = fopen(input, "rb");
   to = fopen(output,"wb");
      while(!feof(from))
      {
         ch = fgetc(from);
         fputc(ch,to);
      }
   fclose(from);
   fclose(to);
}   

_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage

#151620 - darkchild - Sat Mar 01, 2008 4:56 pm

thanks for the help, for the time being I added a simple verification:

Code:
int filecopy(char *input, char *output)
{
   FILE *from, *to;
   char ch;
   from = fopen(input, "rb");
   to = fopen(output,"wb");
   if ((from != NULL)&( to!= NULL))
   {
      while(!feof(from))
      {
         ch = fgetc(from);
         fputc(ch,to);
         
      }
   }
   else
   {
      return -1;
   }
   fclose(from);
   fclose(to);
}   


:) hope it works, if someone else has other verifications worth using, please tell me

#151622 - tepples - Sat Mar 01, 2008 5:34 pm

Four holes I can see:
  1. What happens if 'from' is opened but an error prevents 'to' from opening, or vice versa? It would appear that the function leaks a file descriptor.
  2. Do the strings in 'input' and 'output' get changed inside the function, or can they be qualified with const?
  3. What is returned on a successful call? Is it predictable, or is it undefined (that is, just what happened to be in the first register when the function exits)?
  4. And is it worth calling into cstdio twice for each byte, as opposed to chunks of at least 1 KiB?

Untested; feel free to poke holes in it:
Code:
int copyfile(const char *inPath, const char *outPath) {
  char buffer[1024];
  FILE *infp;
  FILE *outfp;
  int returnValue = 0;

  infp = fopen(inPath, "rb");
  if (!infp) {
    return -1;
  }
  outfp = fopen(outPath, "wb");
  if (!outfp) {
    fclose(infp);
    return -1;
  }

  while(!feof(infp) && returnValue >= 0)
  {
    size_t nRead, nWritten;

    nRead = fread(buffer, 1, sizeof(buffer), infp);
    if (ferror(infp)) {
      returnValue = -1;
    }
    nWritten = fwrite(buffer, 1, nRead, outfp);
    if (ferror(outfp)) {
      returnValue = -1;
    }
  }
  fclose(outfp);
  fclose(infp);

  return returnValue;
}

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.