Updated to use fgets

This commit is contained in:
Steph 2022-12-01 21:01:59 +01:00
parent 1d4ec295f7
commit 9ff8098f52
4 changed files with 19 additions and 13 deletions

Binary file not shown.

View File

@ -4,25 +4,27 @@
int main(void) { int main(void) {
FILE *fp; FILE *fp;
char* line = NULL; char line[255];
size_t len = 0;
int valid = 0; int valid = 0;
fp = fopen("input", "r"); fp = fopen("input", "r");
while(getline(&line, &len, fp) != -1) { if(fp == NULL) {
perror("Error opening file.");
return(-1);
}
while(fgets(line, sizeof(line), fp) != NULL) {
int start, end; int start, end;
char subject; char subject;
char* password; char password[64];
sscanf(line, "%d-%d %c: %s\n", &start, &end, &subject, password); sscanf(line, "%d-%d %c: %s\n", &start, &end, &subject, password);
int occ = 0; int occ = 0;
for(int i=0; password[i] != 0; i++) { for(int i=0; password[i]; i++)
if(password[i] == subject) occ++; if(password[i] == subject) occ++;
}
if(occ <= end && occ >= start) valid++; if(occ <= end && occ >= start) valid++;
} }

Binary file not shown.

View File

@ -3,16 +3,20 @@
int main(void) { int main(void) {
FILE *fp; FILE *fp;
char* line = NULL; char line[255];
size_t len = 0;
int valid = 0; int valid = 0;
fp = fopen("input", "r"); fp = fopen("input", "r");
while(getline(&line, &len, fp) != -1) { if(fp == NULL) {
perror("Error opening file.");
return(-1);
}
while(fgets(line, sizeof(line), fp) != NULL) {
int start, end; int start, end;
char subject; char subject;
char* password; char password[64];
sscanf(line, "%d-%d %c: %s\n", &start, &end, &subject, password); sscanf(line, "%d-%d %c: %s\n", &start, &end, &subject, password);