Reputation: 341
I have written a function as follows to read a text file and write the content into another text file with a different file name:
The read file function:
char *getFileContent (const char *fileName)
{
char errorBuffer[50];
//Prepare read file
FILE *pReadFile;
long bufferReadSize;
char *bufferReadFile; //This variable is going to be returned as file content
size_t readFileSize;
pReadFile = fopen (fileName, "rb");
if (pReadFile != NULL)
{
// Get file size.
fseek (pReadFile , 0 , SEEK_END);
bufferReadSize = ftell (pReadFile);
rewind (pReadFile);
// Allocate RAM to contain the whole file:
bufferReadFile = (char*) malloc (sizeof(char) * bufferReadSize);
if (bufferReadFile != NULL)
{
// Copy the file into the buffer:
readFileSize = fread (bufferReadFile, sizeof(char), bufferReadSize, pReadFile);
if (readFileSize == bufferReadSize)
{
return bufferReadFile;
fclose (pReadFile);
free (bufferReadFile);
} else {
//fread failed
sprintf (errorBuffer, "File reading failed for file:\n%s", fileName);
MessageBox (NULL, errorBuffer, "Error file reading", MB_ICONERROR | MB_OK);
}
} else {
//malloc failed
sprintf (errorBuffer, "Memory allocation failed for file:\n%s", fileName);
MessageBox (NULL, errorBuffer, "Error memory allocation", MB_ICONERROR | MB_OK);
}
} else {
//fopen failed
sprintf (errorBuffer, "File opening failed for file:\n%s", fileName);
MessageBox (NULL, errorBuffer, "Error file opening", MB_ICONERROR | MB_OK);
}
}
The write file code:
//Get file content from read file
char *fileContent = getFileContent (readFileName);
FILE *pWriteFile = fopen (writeFileName, "wb");
fwrite (fileContent, sizeof (char), strlen (fileContent), pWriteFile);
fclose (pWriteFile);
They successfully work together to read and write files. However, in the written file, at the end of it, some strange characters come out like this:
ýýýý««««««««îþîþîþ
Please kindly help me solve this problem. How can I avoid the final strange characters in the written file when they were not there in the original file?
Upvotes: 3
Views: 352
Reputation: 7056
fwrite (fileContent, sizeof (char), strlen (fileContent), pWriteFile);
strlen
() doesn't work here because fileContent contains binary data. The binary data could contain a null byte which would mean strlen
() would be too short, or it may not contain a null byte which means strlen
() would read past fileContent
until it finds a null byte. This would be a reason why you see garbage at the end.
Note also, in your read routine, that the fclose
() and the free
() never happen because they come after the return
statement. But, note that you can't free
() the data until after you write it.
On the other hand, if it's not a binary file, all you need is a terminating 0 at the end of the data and then strlen() would work. So in your read, you need to alloc another byte and make sure that byte is zero:
bufferReadFile = (char*) malloc (sizeof(char) * bufferReadSize + 1); // note the + 1
bufferReadFile[bufferReadSize] = 0; // the terminating null byte.
Upvotes: 3