Reducing file size

This might seem as a stupid question but here is my situation:

I got my linked list to work and can now add/edit/delete/count and all the basic things needed. I also have another file in which data is recorded whenever a new student gets added and or edited.

My problem: When it comes to deleting the problem is how to remove the data from in between the file. I know the exact byte from where the student's record is stored and I also know how long it is by calling sizeof(struct studentRecord).

Assuming sizeof(struct studentRecord) is 80 bytes and the data stored for a particular student starts from the 240th byte of the file. How do I remove the useless data in between the 240th byte and the 320th byte from the file?

My current solution: open the file and write mode (so the complete contents gets deleted) and rewrite the whole data to it again. But this is slower and I'm sure there is a better way of doing this.

Sorry, my English bad.
-unoriginal.
Last edited on
Another way - to fill unused space in file with 0 bytes (or just fill one field of the structure with some specific value). Thus you can easily determine where is the "hole" and when a new student appears you put him there.
I have absolutely no problem as to deciding where to store and where it is stored. I have a separate member in my struct named "pos" which holds the value from where the data was being read.

My only problem is I don't know which function which truncates file length or even better removes specific bytes from in between the files completely. I can add data to a file using fwrite but how do I remove that added data? Without deleting the whole contents of the file of course.

I apologize for my bad english.
-unoriginal
There's no way to deallocate space from file unless to totally rewrite it.
Yes there is. But it is an OS-dependent feature.

In the Unix world, use truncate():
http://linux.die.net/man/2/truncate

In the Windows world, use SetEndOfFile():
http://www.google.com/search?btnI=1&q=msdn+SetEndOfFile

Both techniques require gaining an OS file handle and dinking with the file that way. If you wish to do this, I would write a function that takes a file name and a new size that does it.

Good luck!

In the Unix world...

In the Windows world...

What about those lost and distressed Mac users lol?
Mac is a Unix
Ok, I tried using SetEndOfFile but nothing much happened. Firstly, I'm using fopen, fclose, fwrite etc for I/O with the file. So I had to use a whole new set of functions with SetEndOfFile :/

I had to search a bit for finding appropriate functions that use the HANDLE data type.
I have never used these before:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

const char *fname = "SetEndOfFile_.test";

void error(const char *errMsg)
{
	printf("ERROR: %s\nERROR CODE: %lu", errMsg, GetLastError());
	getch();
	exit(EXIT_FAILURE);
}

int main()
{
	HANDLE hFile;
	const char buff[] = "0123456789"; // 10 bytes
	DWORD bytesWritten;
	long whoa;

	hFile = CreateFile(fname,
                   GENERIC_READ | GENERIC_WRITE,
                   FILE_SHARE_READ | FILE_SHARE_WRITE,
                   NULL,
                   CREATE_ALWAYS,
                   FILE_ATTRIBUTE_NORMAL,
                   NULL); // kimi o ai shiteru, fopen. <3
	if(hFile == INVALID_HANDLE_VALUE)
		error("CreateFile failed");

	if(!WriteFile(hFile, buff, sizeof(buff)-1, &bytesWritten, NULL)) //fwrite
		error("WriteFile failed");

	if(!SetFilePointer(hFile, 5L, &whoa, FILE_BEGIN)) //Set file pointer to move forward by 5 bytes. 
                error("SetFilePointer failed.");
	if(!SetEndOfFile(hFile)) // hmmmm......
		error("SetEndOfFile failed.");

	CloseHandle(hFile);
	getch();
	return 0;
}


The code didn't work as I had expected it to. Only 5 bytes should have been in the file after opening but it wasn't so. All the 10 characters were there.


Duoas, it would be greatly helpful if you could write the function you are talking about in your post. I use windows and my teacher wouldn't like a work that doesn't compile on windows.

I'll stick to my old solution of rewriting the complete file by opening in write mode if nothing comes out.

Thanks for all of your replies and attention.
-unoriginal.
Last edited on
Topic archived. No new replies allowed.