-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
47 lines (38 loc) · 1.03 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#define BUFFER_SIZE (16 * 1024)
void usage(char* argv[]) { printf("Usage: %s inputFile.txt\n", argv[0]); }
uint64_t lineCount(char* start, char* end) {
int lineCounter = 0;
while ((start = (char*)memchr(start, '\n', end - start))) {
++lineCounter;
++start;
}
return lineCounter;
}
int main(int argc, char* argv[]) {
FILE* pFile;
size_t bytesRead;
char buffer[BUFFER_SIZE];
uint64_t lineCounter, totalRead;
lineCounter = totalRead = 0;
if (argc < 2) {
usage(argv);
return EXIT_FAILURE;
}
pFile = fopen(argv[1], "r");
if (pFile == NULL) {
fputs("File error", stderr);
return EXIT_FAILURE;
}
while ((bytesRead = fread(buffer, sizeof(char), BUFFER_SIZE, pFile)) > 0) {
totalRead += bytesRead;
lineCounter += lineCount(buffer, buffer + bytesRead);
}
printf("lines:%"PRIu64" bytes:%"PRIu64" file:%s\n", ++lineCounter, totalRead, argv[1]);
fclose(pFile);
return EXIT_SUCCESS;
}