105 lines
1.9 KiB
C
105 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
struct bingocard
|
|
{
|
|
char *card[5][5];
|
|
int hits[5][5];
|
|
char *name;
|
|
};
|
|
|
|
static int
|
|
getline(char* line, FILE* fp, int length)
|
|
{
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
line[i] = fgetc(fp);
|
|
if (line[i] == EOF)
|
|
return -1;
|
|
}
|
|
line[length-1] = '\0';
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
getlinelength(FILE *fp)
|
|
{
|
|
int linelength = 0;
|
|
long offset = ftell(fp);
|
|
for (;;)
|
|
{
|
|
if (fgetc(fp) == '\n')
|
|
{
|
|
linelength += 1;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
linelength += 1;
|
|
}
|
|
}
|
|
fseek(fp, offset, SEEK_SET); // seek back to beginning of line
|
|
return linelength;
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
printf("Opening input.txt\n");
|
|
FILE *fp = fopen("input.txt","r");
|
|
if (fp == NULL)
|
|
{
|
|
printf("Could not open file: input.txt\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// First line of file is all the numbers drawn
|
|
char *drawsline = (char *) malloc(getlinelength(fp)+1);
|
|
getline(drawsline, fp, getlinelength(fp)+1);
|
|
|
|
// Figure out how many numbers are drawn in total
|
|
int numelements = 0;
|
|
for (size_t i = 0; i < strlen(drawsline); i ++)
|
|
if (strncmp(&drawsline[i], ",", 1) || drawsline[i] == '\0')
|
|
numelements += 1;
|
|
printf("There are %d numbers drawn\n", numelements);
|
|
|
|
// Create an array from that string
|
|
char **draws = (char **) malloc(numelements * sizeof(char));
|
|
if (draws == NULL)
|
|
{
|
|
printf("Unable to allocate memory for drawn values.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// THIS BREAKS THINGS ON WINDOWS FOR SOME REASON???
|
|
// NOT INFINITE LOOP, PROGRAM EXITS BUT DOESN'T PRINT
|
|
// ANYMORE
|
|
// ... Maybe I should write my own strtok
|
|
char *token = strtok(drawsline, ",");
|
|
int index;
|
|
while (token != NULL)
|
|
{
|
|
draws[index++] = token;
|
|
printf("Consumed: %s\n", token);
|
|
token = strtok(NULL, ",");
|
|
}
|
|
|
|
printf("[");
|
|
for (int i = 0; i < numelements; i++)
|
|
printf("%s,", draws[i]);
|
|
printf("]\n");
|
|
|
|
// Now parse bingo cards
|
|
|
|
fclose(fp);
|
|
if (draws)
|
|
{
|
|
free(draws);
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
|
}
|