I want to write a C program, which could insert one line into the head of file.
However, if the file is very big, I need to read() all the file content, then add one line in the head, then write() them back.
Is there some way could be more effiency??
My Program is like below
int main(int argc, char **argv)
{
char buffer[MAX_LENGTH];
const char *line = argv[1];
int fd = open("bigfile.txt", O_RDWR);
read(fd, buffer, MAX_LENGTH);
write(fd, line, strlen(line));
write(fd, buffer, strlen(buffer)); //really low efficient, I need to rewrte the whole file every time
}