Skip to content

Instantly share code, notes, and snippets.

@lgrz
Created August 24, 2015 19:18
Show Gist options
  • Save lgrz/9343df4bae0d08adca31 to your computer and use it in GitHub Desktop.
Save lgrz/9343df4bae0d08adca31 to your computer and use it in GitHub Desktop.
/* gcc -g -o readlex readlex.c -Isrc */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "index.h"
int
main(void)
{
int i;
FILE *fp;
struct lexicon_entry lex_entry;
if (!(fp = fopen("lexicon", "r"))) {
perror("fopen");
return EXIT_FAILURE;
}
for (i = 0; i < 5; ++i) {
memset(&lex_entry, 0x0, sizeof(struct lexicon_entry));
/*
* Read in the first three members of the lexicon_entry struct.
*
* 4 + 4 + 2 bytes
*/
fread(&lex_entry, sizeof(lex_entry.doc_freq) + sizeof(lex_entry.offset)
+ sizeof(lex_entry.len), 1, fp);
/* allocate memory for the `term` member */
lex_entry.term = (char *)calloc(1, lex_entry.len + 1);
fread(lex_entry.term, lex_entry.len, 1, fp);
/*
* using `calloc` above means we already have a `\0` char terminating our
* string, the following is not necessary, but let's do it anyway.
*/
lex_entry.term[lex_entry.len] = '\0';
printf("lex_entry.doc_freq: %d\n", lex_entry.doc_freq);
printf("lex_entry.offset: %d\n", lex_entry.offset);
printf("lex_entry.len: %d\n", lex_entry.len);
printf("lex_entry.term: %s\n", lex_entry.term);
free(lex_entry.term);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment