Skip to content

Instantly share code, notes, and snippets.

@erik
Created December 27, 2010 00:29
Show Gist options
  • Save erik/755756 to your computer and use it in GitHub Desktop.
Save erik/755756 to your computer and use it in GitHub Desktop.
static char* ReadDelim(FILE* fp, char delim) {
char c;
int i = 0;
fgetc(fp);
long int pos = ftell(fp);
while((c = fgetc(fp))) {
ungetc(c, fp);
fseek(fp, 1, SEEK_CUR);
if(c == EOF || feof(fp)) {
isEOL = isEOF = 1;
break;
} else if(c == delim) {
i++;
break;
} else {
if(c == '\n' || c == '\r') {
isEOL = 1;
} else {
isEOL = 0;
}
isEOF = 0;
i++;
}
}
fseek(fp, pos, SEEK_SET);
char *str = malloc(i - 1);
fread(str, 1, i - 1, fp), strlen(str);
// get rid of last delim
fgetc(fp);
printf("ohai, i just read: >>%s<<\n", str);
if(isEOF) {
fprintf(stderr, "Unterminated delim ('%d'): %s\n", delim, str);
return NULL;
}
return str;
}
/* --- File being read --- */
( this should be 5 ) 2 2 * 1 + .
( this should print "hello world") " Hello World" . ( yes)
/* --- some output --- */
ohai, i just read: >>this should be 5 <<
...
ohai, i just read: >>this should print "hello world"<<
ohai, i just read: >>Hello World<<
Token added: Hello World
Token added: .�n�8�n�
Token added: (�n���n��q1
Token added: yes)��n�
Unknown word: .�n�8�n�
Unknown word: (�n���n��q
Unknown word: yes)��n�
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment