Skip to content

Instantly share code, notes, and snippets.

@RouNNdeL
Created June 18, 2020 12:30
Show Gist options
  • Save RouNNdeL/e2dd1dd5a8dd29f4f348ccd27430162a to your computer and use it in GitHub Desktop.
Save RouNNdeL/e2dd1dd5a8dd29f4f348ccd27430162a to your computer and use it in GitHub Desktop.
Word list prefix filter
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool matches(const char *pre, const char *str) {
return strncmp(pre, str, strlen(pre)) == 0;
}
int main(int argc, char *argv[]) {
if (argc < 4) {
printf("Usage: %s <prefix> <input> <output>", argv[0]);
return -1;
}
char *readFile = argv[2];
char *writeFile = argv[3];
FILE *read = fopen(readFile, "r");
FILE *write = fopen(writeFile, "w");
if (read == NULL) {
printf("Cannot open file for reading");
return -1;
}
if (write == NULL) {
printf("Cannot open file for writing");
return -1;
}
char line[256];
while (fgets(line, sizeof(line), read)) {
if (matches(argv[1], line)) {
fputs(line, write);
}
}
fclose(read);
fclose(write);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment