I was playing around today stdio.h functions and one thing that i was trying to do is to delete the last character from a file but i can't see a function in the cstdio that would let me do it. So my question is it possible and if yes how do I go about this?
Setting the file size is the only feasible way to do this efficiently.
This is done using SetEndOfFile or _chsize in Windows and ftruncate/ftruncate64 on other operating systems.
You could dig into the filesystem and alter the records of the file, changing it so that the size of the file is reported as one character less. This ranges from quite easy to quite difficult, depending on your filesystem and operating system.
fseek(f1, 0, SEEK_END);
long Size = ftell(f1);
rewind(f1);
buffer=(char*) malloc(sizeof(char)*Size);
if(buffer==NULL){
MessageBoxA(NULL,"Memory allocation has failed!", "ERROR!", MB_OK | MB_ICONEXCLAMATION);
return 0;
}
size_t result = fread(buffer, 1, Size, f1);
if(result!=Size){
MessageBoxA(NULL,"Error while reading the file!", "ERROR!", MB_OK | MB_ICONEXCLAMATION);
return 0;
}
remove("test.txt");
f2 = fopen("test.txt","a+");
for(int y=0; y<(sizeof(buffer)-sizeof(char)); y++){
fwrite(buffer, (sizeof(buffer)-1), 1, f2);
}
f1 = f2;
The above code I made according to the first tip and well it gives me a wierd content of the test file in a manner where the whole text stays the same and plus the first three letters of the file are being inserted at the end of it three times instead of removing the last character :|
thanks man i actually now changed it and it works well i mean the removing of one char but the problem now is that it actually adds on the whole content to the end of the file with the one char removed to the actual content of the file so it seems like