forked from sysprog21/phonebook-concurrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_align.c
55 lines (40 loc) · 1 KB
/
file_align.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUFF_SIZE 100
static off_t fsize(char *path);
/* usage:
* ./main orgfName modfName Padded
*/
int main(int argc, char *argv[])
{
assert(argc == 4 && "./main orgfName modfName Padded");
char *org = argv[1];
char *mod = argv[2];
int pad = atoi(argv[3]);
printf("orginal file size = %ld\n", fsize(org));
FILE *fd0 = fopen(org, "r");
FILE *fd1 = fopen(mod, "w+");
char rbuf[MAX_BUFF_SIZE];
int suffix;
char *wbuf = (char *) malloc(sizeof(char) * pad);
while (fgets(rbuf, sizeof(rbuf), fd0)) {
memset(wbuf, '\0', pad);
if ((suffix = (pad - strlen(rbuf))) != 0)
strncpy(wbuf, rbuf,strlen(rbuf));
fwrite(wbuf, pad, 1, fd1);
}
fclose(fd0);
fclose(fd1);
return 0;
}
static off_t fsize(char *path)
{
struct stat st;
stat(path, &st);
return st.st_size;
}