-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappend2.c
41 lines (31 loc) · 923 Bytes
/
append2.c
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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char* argv[]) {
int fd;
char appendIn[250];
//Opening file via command line argument
fd = open(argv[1], O_RDWR | O_APPEND);
//Getting filesize via lseek.
off_t fileSize = lseek(fd, 0, SEEK_END);
//Buffer
char buffer[fileSize+1];
//Setting pointer back to beginning of the file.
lseek(fd, 0, SEEK_SET);
//Reading and displaying the file contents.
read(fd, buffer, fileSize);
write(1, buffer, fileSize);
printf("Enter Data to be Appended in the file: \n");
//Appendi input
fgets(appendIn, 250, stdin);
write(fd, appendIn, fileSize);
printf("Data appended:\n");
fileSize = lseek(fd, 0, SEEK_END);
//Again, setting seek to the beginning of the file.
lseek(fd, 0, SEEK_SET);
//Printing the whole file agian, after appending.
read(fd, buffer, fileSize);
write(1, buffer, fileSize);
close(fd);
return 0;
}