Removing last character using cstdio

Jun 27, 2011 at 8:27pm
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?
Jun 27, 2011 at 8:32pm
Copy the file, omitting the last character, and then delete the original.
Jun 27, 2011 at 9:40pm
Is there any easier or different way to do it??
Jun 27, 2011 at 9:51pm
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.
Jun 27, 2011 at 9:53pm
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 Jun 27, 2011 at 9:56pm
Jun 27, 2011 at 11:46pm
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 :|
Jun 28, 2011 at 12:05am
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 Jun 28, 2011 at 12:10am
Jun 28, 2011 at 12:07am
and now it ads on only once the first 3 chars to the end of the content instead of deleting one char...
Jun 28, 2011 at 12:23am
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;
    }
  }

Jun 28, 2011 at 12:30pm
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?
Jun 28, 2011 at 12:42pm
sizeof(char) is one. Always. It's defined in the C++ standard.
Jun 28, 2011 at 1:00pm
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 Jun 28, 2011 at 1:02pm
Topic archived. No new replies allowed.