Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Counts the amount of lines (\n) in given file
- * @param f The file to be used
- * @return The number of lines in the file
- */
- int getNumberOfLines(FILE *f) {
- int count = 0;
- //start from the beginning
- fseek(f, 0, SEEK_SET);
- //read
- char * buf = new char[BUFFERING_BLOCK_SIZE];
- for (;;) {
- //read a nice chunk (to minimize head seek overhead)
- size_t amount_read = fread(buf, sizeof(char), BUFFERING_BLOCK_SIZE, f);
- if (amount_read == 0)
- break;
- //count occurrences of '\n' in that chunk
- for (size_t i = 0; i < amount_read; i++) {
- if (buf[i] == '\n')
- count++;
- }
- }
- free(buf);
- // if (DEBUG)
- // cout << "Number of lines:\t" << count << endl;
- return count;
- }
Add Comment
Please, Sign In to add comment