Removing last character using cstdio

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?
Copy the file, omitting the last character, and then delete the original.
Is there any easier or different way to do it??
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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 :|
I removed that loop cause i don't even know why i put it there so to make it easier for you it looks like this now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
				f1 = fopen("test.txt","a+");
				fseek(f1, 0, SEEK_END);
				long Size = ftell(f1);
				rewind(f1);

				buffer=(char*) malloc(sizeof(char)*Size);
				size_t result = fread(buffer, 1, Size, f1);


				remove("test.txt");
				f2 = fopen("test.txt","a+");

				
				fwrite(buffer, (sizeof(buffer)-sizeof(char)), 1, f2);
				
				f1 = f2;

Last edited on
and now it ads on only once the first 3 chars to the end of the content instead of deleting one char...
That doesn't really make any sense. sizeof doesn't do what you seem to think it does.

That's how you should do it (well, in real code all these calls should be safely hidden behind appropriate wrappers):
1
2
3
4
5
6
7
8
9
10
  HANDLE fileHandle=CreateFileA("foo.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
  LARGE_INTEGER size;
  if (fileHandle!=INVALID_HANDLE_VALUE && GetFileSizeEx(fileHandle,&size))
  {
    if (size.QuadPart>0)size.QuadPart--;
    if (SetFilePointerEx(fileHandle,size,NULL,FILE_BEGIN) && SetEndOfFile(fileHandle))
    {
      cout << "Success" << endl;
    }
  }

it seems like my project cant obtain the handle of the file... :/ is it the only rule that the file has to be in the same location as exe file?
sizeof(char) is one. Always. It's defined in the C++ standard.
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
1
2
remove("test.txt");
f2 = fopen("test.txt","a+");
doesnt work
Last edited on
Topic archived. No new replies allowed.