Skip to content

Instantly share code, notes, and snippets.

@atroche
Last active May 18, 2017 21:25
Show Gist options
  • Save atroche/fe757cc547cace688a65324fa8f99de9 to your computer and use it in GitHub Desktop.
Save atroche/fe757cc547cace688a65324fa8f99de9 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <fcntl.h>
#include <err.h>
#include <unistd.h>
#include <inttypes.h>
#define BUF_SIZE (1048576 * 10)
int main(int argc, char *argv[])
{
static char char_buf[BUF_SIZE];
static ssize_t buf_size = BUF_SIZE;
void *buf = char_buf;
char *p;
ssize_t len;
uint64_t linect;
int fd = open("ten.json", O_RDONLY);
if (fd < 0) {
warn("bad open");
return 1;
}
/*
while((len = read(fd, buf, buf_size))) {
for(p=buf;len--;p++){
if(*p == '\n'){
linect++;
}
}
}
*/
// faster!
while((len = read(fd, buf, buf_size))) {
for(p=buf;len>0;p+=2){
if(*p == '\n'){
counta++;
}
if(*(p+1) == '\n'){
countb++;
}
len -=2;
}
}
printf("%" PRIu64 "\n", linect);
}
@rbeesley
Copy link

rbeesley commented Mar 3, 2017

Not sure how this compiles and does anything. Line 37 and line 40 reference undeclared variables counta and countb, neither of which are listed as computing linect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment