Hi, Is there any way to Delete last 10 characters from a text file ?
Thanks
Hi, Is there any way to Delete last 10 characters from a text file ?
Thanks
For single-byte encoding on POSIX platform you can use something like this (error handling omitted):
FILE *file = fopen("filename", "a");
fseek(file, -10, SEEK_END);
ftruncate(fileno(file), ftell(file)); // POSIX function
It is not going to work for encodings with variable-length characters, such as UTF-8 and UTF-16.
For something that will work under windows as well you could do something like this:
FILE* pFileIn = fopen( "filenameIn", "rb" );
FILE* pFileOut = fopen( "filenameOut", "w+b" );
fseek( pFileIn, -10, SEEK_END );
long length = ftell( pFile );
long blockSize = 16384;
void* pBlock = malloc( blockSize );
long dataLeft = length;
while( dataLeft > 0 )
{
long toCopy = (dataLeft > blockSize) ? blockSize : dataLeft;
fread( pBlock, toCopy, 1, pFileIn );
fwrite( pBlock, toCopy, 1, pFileOut );
dataLef -= toCopy;
}
free( pBlock );
fclose( pFileIn );
fclose( pFileOut );