Kyle R . Burton on Thu, 20 Jun 2002 00:01:23 -0400


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] this code crashes [OT]


> Well great, but I still need to write to this file.
> 
> I'm going to start looking over this class as well. I guess blew my
> chances of ever being hired as a c++ coder for anyone in plug. Shh. I got
> this job on the basis of other code that I made so I don't feel that I
> misrepresented or anything. :)
> 
> I checked and double checked the code I have. I don't see a bug that would
> segfault, though I mentally predicted that messy loop problem (and tried
> to fix it), a problem, but not enough to segfault.

At the risk of sticking my neck out (and posting code for the list to 
criticize), here's a working 'copyFile' in C (loosely based on your
code):


#include <stdio.h>
#include <string.h>
#include <errno.h>

#define MAX_BUFF 1024

int copyFile( const char *sourceFile, const char* destFile )
{
  char buff[MAX_BUFF];
  FILE *src, *dst;
  size_t bytes_read;
  int rv = 0;

  src = fopen( sourceFile, "r" );
  if( NULL == src ) {
    fprintf(stderr,"%s(%d) Error opening file '%s' for reading: %s\n",
      __FILE__,__LINE__,sourceFile,strerror(errno));
    return 0;
  }

  dst = fopen( destFile, "w" );
  if( NULL == dst ) {
    fprintf(stderr,"(%s)%d Error opening file '%s' for writing: %s\n",
      __FILE__,__LINE__,sourceFile,strerror(errno));
    fclose( src );
    return 0;
  }

  while(1) {
    bytes_read = fread( buff, 1, MAX_BUFF, src ); 
    if( 0 == bytes_read && feof(src) ) {
      break;
    }

    if( 0 == bytes_read && ferror(src) ) {
      fprintf(stderr,"%s(%d) error reading from file: '%s' : %s\n",
        __FILE__,__LINE__,sourceFile,strerror(errno));
      rv = errno;
    }
    if( bytes_read != fwrite( buff, 1, bytes_read, dst ) ) {
      fprintf(stderr,"%s(%d) error writing %d bytes to file: '%s' : %s\n",
        __FILE__,__LINE__,bytes_read,sourceFile,strerror(errno));
      rv = errno;
      break;
    }
  }

  fclose(src);
  fclose(dst);

  return rv; 
}

int main( int argc, char** argv )
{
  char *bin, *src, *dst;
  bin = *argv++; argc--;
  if( argc != 2 ) {
printf("argc: %d\n",argc);
    printf("Error, you must specify source and destination files!\n");
    return -1;
  }
  src = *argv++; argc--;
  dst = *argv++; argc--;
  printf("%s copying %s => %s\n",bin,src,dst);
  if( 0 != copyFile(src,dst) ) {
    printf("Error copying file!\n");
  }
  else {
    printf("file copied.\n");
  }
  return 0;
}



(hopefuly I won't loose any job opportunities :)

Kyle

-- 

------------------------------------------------------------------------------
Wisdom and Compassion are inseparable.
        -- Christmas Humphreys
mortis@voicenet.com                            http://www.voicenet.com/~mortis
------------------------------------------------------------------------------

______________________________________________________________________
Philadelphia Linux Users Group       -      http://www.phillylinux.org
Announcements-http://lists.phillylinux.org/mail/listinfo/plug-announce
General Discussion  -  http://lists.phillylinux.org/mail/listinfo/plug