Skip to content

Instantly share code, notes, and snippets.

@edomora97
Last active August 29, 2015 14:05
Show Gist options
  • Save edomora97/e8186c1a101840679a73 to your computer and use it in GitHub Desktop.
Save edomora97/e8186c1a101840679a73 to your computer and use it in GitHub Desktop.
C++ Fast file reader
16
1
2 3
4 5 6
7
8 9
10 11
12
13 14 15
1234567890
5
123456789123456
135792648013579 101010101010101
120120120120120120 210210210210210210
5
abcdefghijklme AMcvdinVCWew
.-,vè092367][``~€¶Ð§łß
aknafsna 15f3d1bg35fgb
/*
* (really) FAST READING
*
* How to:
* 1. Set the buffer size: BUFFER_SIZE = ???
* 2. Open the file: _fd = open("filename.txt", O_RDONLY);
* 3. Load the buffer: loadBuffer();
* -------------
* 4. Read int: int x = readint();
* 5. Read long long int: long long y = readlonglong();
* 6. Read string (no space): int len = readstring(str);
*/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFFER_SIZE 500
#define DEBUG 0
#if DEBUG
#define _D(x) x
#else
#define _D(x)
#endif
char _buffer[BUFFER_SIZE];
int _buf_pointer;
int _buf_size;
int _fd;
// load the buffer
void loadBuffer() {
_buf_size = read(_fd, _buffer, BUFFER_SIZE);
_buf_pointer = 0;
_D(printf("load buffer: %d chars loaded\n", _buf_size);)
}
// skip all spaces
int skipspaces() {
char c;
// skip spaces
while (((c = _buffer[_buf_pointer++]) == ' ' || c == '\n' || c == '\t') && c != '\0') {
_D(printf("Skipped: %d\n", (int)c);)
if (_buf_pointer >= _buf_size)
loadBuffer();
}
// if EOF stop
if (c == '\0') {
_D(printf("EOF!!!\n");)
return 0;
}
// unskip last char
_buf_pointer--;
return 1;
}
// read an int from the buffer
int readint() {
int n = 0;
char c;
if (!skipspaces()) return -2000000000;
// parse the int from the buffer
do {
c = _buffer[_buf_pointer++];
if (_buf_pointer >= _buf_size) loadBuffer();
if (c >= '0' && c <= '9')
n = n*10 + (c-'0');
} while (c >= '0' && c <= '9');
return n;
}
// read a long long int from the buffer
long long readlonglong() {
long long n = 0;
char c;
if (!skipspaces()) return -2000000000000000;
// parse the int from the buffer
do {
c = _buffer[_buf_pointer++];
if (_buf_pointer >= _buf_size) loadBuffer();
if (c >= '0' && c <= '9')
n = n*10 + (c-'0');
} while (c >= '0' && c <= '9');
return n;
}
// read a string from the buffer
int readstring(char* str) {
char c;
int l = 0;
if (!skipspaces()) {
str[0] = 0;
return 0;
}
// read the string
do {
c = _buffer[_buf_pointer++];
str[l++] = c;
if (_buf_pointer >= _buf_size) loadBuffer();
// until a space
} while (c != ' ' && c != '\n' && c != '\t' && c != '\0');
// string terminator
str[--l] = '\0';
return l;
}
int main() {
// see the sample input.txt!!
_fd = open("input.txt", O_RDONLY);
loadBuffer();
int N = readint();
printf("N = %d\n", N);
while (N--)
printf("%d\n", readint());
int M = readint();
printf("M = %d\n", M);
while (M--)
printf("%lld\n", readlonglong());
int S = readint();
char str[100];
printf("S = %d\n", S);
while (S--) {
int l = readstring(str);
printf("%s [%d charaters]\n", str, l);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment