Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- #include <fcntl.h>
- #include <vector>
- bool die(const char *s)
- {
- perror(s);
- exit(1);
- }
- int main(int argc,char **argv)
- {
- char c;
- int num=0;
- int pos=1;
- int line=1;
- std::vector<int> res;
- struct stat statbuf;
- int fd=open(argv[1],O_RDONLY);
- (fd>0) || die("open");
- (fstat(fd,&statbuf)==0) || die("stat");
- void *mem=mmap(0,statbuf.st_size,PROT_READ,MAP_SHARED,fd,0);
- mem || die("mmap");
- char *head=(char*)mem;
- char *tail=head+statbuf.st_size;
- while((c=*head++),(head<=tail))
- {
- if (c>='0' && c<='9')
- num=num*10+c-'0';
- else if (c=='-') pos=0;
- else if (c=='\n')
- {
- num=pos?num:-num;
- res.push_back(num);
- num=0;
- pos=1;
- line++;
- }
- else
- {
- printf("I've got a problem with this file at line %d\n",line);
- exit(1);
- }
- }
- unsigned sum=0;
- for (int i=0;i<res.size();i++)
- {
- sum=sum+(unsigned)res[i];
- }
- printf("=>%d\n",sum);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement