Created
June 18, 2020 12:30
-
-
Save RouNNdeL/e2dd1dd5a8dd29f4f348ccd27430162a to your computer and use it in GitHub Desktop.
Word list prefix filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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