Skip to content

Instantly share code, notes, and snippets.

@joyeecheung
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyeecheung/49360a0b050f3803d454 to your computer and use it in GitHub Desktop.
Save joyeecheung/49360a0b050f3803d454 to your computer and use it in GitHub Desktop.
Read until the character `?`
#include <stdio.h>
#include <string.h>
int main(void) {
char line[30000];
char str[30000];
FILE * pFile = fopen ("myfile.txt" , "r");
/* Avoid buffer overflow by using fgets() */
while (fgets(line, sizeof(line), pFile) != 0) {
if (sscanf(line, "%[^?]s", str) != 1) /* match [^?] */
break; /* stop for this line if the pattern doesn't match */
/* print the result and the length */
printf("Found <<%s>>\n", str);
printf("The size of str is %d\n", strlen(str));
}
int ch; /* faliure position */
if ((ch = getc(pFile)) != EOF)
printf("Failed on character %d (%c)\n", ch, ch);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment