Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Why the shit doesn't this work?

Name: Anonymous 2020-12-24 3:41

The following code is supposed to take a .txt file like the one below:
https://pastebin.com/HfjkVZpy
It will then go by each line and see if each password is valid by the amount of times the letter appears. For example, "1-9 a: aaaer" is valid, but "1-2 q: aqeeqgqf" is not".
#include <stdio.h>
#include <string.h>

int main( int argc, char *argv[])
{
FILE *file;
int i = 0;
int file_line_count;
int valid_passwords = 0;
file = fopen(argv[1], "r");
typedef struct node {
char ranges[4];
char filter_letter[3];
char password[51];
} Password_Verification;

if (file) {
for (char c = getc(file); c != EOF; c = getc(file)) {
if (c == '\n') file_line_count = file_line_count + 1;
}
Password_Verification Pswds[file_line_count + 1];
while (3 == fscanf(file, " %4c %3c %51c", &Pswds[i].ranges,
&Pswds[i].filter_letter,
&Pswds[i].password))
{

int k = 0;
for (int j = 0; j < strlen(Pswds[i].password); j++) {
if (Pswds[i].password[j] == Pswds[i].filter_letter[0]) {
k = k + 1;
if ( (k >= (int)(Pswds[i].ranges[0] - 48)) && (k <= (int)(Pswds[i].ranges[2] - 48)) ) {
valid_passwords = valid_passwords + 1;
}

}
continue;
}
}
printf("%d\n", valid_passwords);
return 0;
}
}


But for some reason, when I run this code with the input above, the valid_passwords factor never increments. Why is this the case?

Name: Anonymous 2020-12-29 5:57

Code tags look like [ c o d e ] ... [ / c o d e ]

// https://dis.tinychan.net/read/prog/1608781261
// probably based on https://adventofcode.com/2020/day/2 [part 2 not shown]

#include <stdio.h>
// #include <string.h> // use format strings don't implement parser
#include <stdlib.h>

// fuck this shit...
// scanf() format string "%*s" is a check & skip,
// without return count. not a field width.
// use fixed field width (SZ-1 because of '\0')
#define SZ 128
#define GETLINE(lo, hi, ch, pass) scanf("%d-%d %c: %127s", &lo, &hi, &ch, pass)
#define FIELDS 4

#define SHOWBAD 1

#define DEBUG 0
#define LOG(fmt, ...) fprintf(stderr, "TRACE: " fmt, __VA_ARGS__)

int count(char ch, char *str) {
int sum = 0;
if(!str || !*str) return 0;
for(int i = 0; str[i]; i++) {
if(ch == str[i]) sum++;
}
return sum;
}

int main(int argc, char *argv[]) {
int ch, cnt, lo, hi;
char pass[SZ];

if(argc > 1) {
FILE *f = freopen(argv[1], "r", stdin);
if(DEBUG) LOG("FILE = %p, stdin = %p\n", f, stdin);
if(!f || f != stdin) { perror("can't open password file!"); exit(1); }
}

int line = 0, valid = 0, invalid = 0;
int x = GETLINE(lo, hi, ch, pass);
if(DEBUG) LOG("x = %d\n", x);

while(x == FIELDS) {
line++;
cnt = count(ch, pass);
if(DEBUG) LOG("[%d-%d] %c=%d: %s\n", lo, hi, ch, cnt, pass);

if(lo <= cnt && cnt <= hi) valid++;
else {
if(SHOWBAD) printf("BAD!!! %d-%d %c: '%s' BAD!!! line: %d\n", lo, hi, ch, pass, line);
invalid++;
}

x = GETLINE(lo, hi, ch, pass);
if(DEBUG) LOG("x = %d\n", x);
}

printf("[%d] valid passwords [%d bad]\n", valid, invalid);
return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List