Copying data in SD card with PROS (Solved)

I’m having some trouble with a simple function to copy a file in PROS on the SD card. The function in question:

void copyFile(const char* sourceFileName, const char* newFileName)
{
 char tempChar;

 FILE * readFile = fopen(sourceFileName,"r");
 FILE * writeFile = fopen(newFileName,"w");

 tempChar = fgetc(readFile);

 while(tempChar != EOF)
 {
   fputc(tempChar,writeFile);
   tempChar = fgetc(readFile);
 }

 fclose(readFile);
 fclose(writeFile);
}

The program does not crash but just freezes and becomes unresponsive. When I stop the program and take out the SD the new file is created, but no data is written in it. I’m assuming it’s a problem with the while() loop but I’m not sure why. Also the SD card I’m using is: SD card

not sure exactly what the problem is, but tempChar should be an int and you should check that the file pointers are valid before using them. This is how I might write it in VEXcode.

char buf[100];

void
copyTest( const char *path1, const char *path2 ) {
    FILE *fp1 = fopen(path1, "r");
    
    if( fp1 != NULL ) {
      FILE *fp2 = fopen(path2, "w");
      if( fp2 != NULL ) {
          int c;

          while( (c = fgetc(fp1) )!= EOF ) {
            fputc(c, fp2 );
          }

          fclose(fp1);
          fclose(fp2);
      }
    }
}

int main() {
    FILE *fp = fopen("test.txt", "w" );

    if( fp != NULL ) {
      int nWritten = fwrite( buf, 1, 100, fp );
      printf("%d\n", nWritten);
      fclose(fp);

      copyTest("test.txt", "test1.txt");
    }
}
9 Likes

The problem was having it as a char value, thank you! Just look it up and EOF is commonly defined with a -1 value, so I would venture to guess char values are unsigned by default in this compiler causing it to infinitely loop.

1 Like