Hi,
I've made a function that reads in a file by first determining the file size and then reading in the contents, unfortunately for some reason when the contents are read in, additional unwanted and non existent characters are being read in to (characters that aren't in the files contents), I can't see where the problem is, can anyone spot what I've done incorrectly?
Code:
void encrypt(char *key,char *fileIn,char *fileOut)
{
FILE *pFile;
long fileSize;
if( (pFile = fopen ( fileIn, "r")) != NULL )
{
fseek (pFile,0,SEEK_END);
fileSize= ftell (pFile);
} else {
printf( "File could not be opened\n" );
}
fclose(pFile);
char message[fileSize];
if( (pFile = fopen( fileIn, "r" )) != NULL )
{
fread( message, sizeof( char ), (fileSize), pFile );
printf( "Number of characters: %d\n", fileSize );
printf( "Message reads: %s\n", message );
fclose( pFile );
}
else {
printf( "File could not be opened\n" );
}
}
The function itself is incomplete I'm just trying to get the read in part of it correct first, the idea is that it will read in the contents of a plain text file into an array, alter the data and then output it to separate file (cipher file).
at the moment when I read in a file containing the word Test, I get:
Test (which is the contents followed by a square thing then >>-u#E and a few others I can't make on this keyboard.
Any help is appreciated.