Originally Posted by
Structure
Why not skip the buffer completely? It's not necessary:
Code:
#include <stdio.h>#include <stdbool.h>
int main(int args, char *argv[]) {
FILE *fp = fopen("text.txt", "r");
bool writeChar = false;
int ch;
while ((ch = fgetc(fp)) != EOF) {
if (ch == ']') {
writeChar = false;
putchar('\n');
}
if (writeChar) {
putchar(ch);
}
if (ch == '[') writeChar = true;
}
fclose(fp);
return 0;
}
(Also incorporating other suggestions in this thread, e.g., make ch an int and rename readChar to writeChar.)
text.txt:
Code:
foo[This string makes Structure's code crash and burn]foo
Code:
$ ./178682
This string makes Structure's code crash and burn