/* Assignment #2 - Fall 2024 This program processes a file, storing information about each line in a struct within an array of structs. To compile this program: gcc -Wall -Werror -g -o a2 a2.c To run this program, you must specify 1 argument (the filename): ./a2 a2_data.txt */ #include #include #define MAX_LINES 100 #define DEBUG 1 /* * Stats for a given line in a file will be stored together in a struct. */ typedef struct { char first; char last; int length; } line_stats_t; /* * Reads in the given file and stores statistics for each line. * * Inputs: * - file_name: the name of the file (as a C "string") * - stats: the array of line_stats_t, with space for one entry per line * * Returns: number of lines parsed if successful, or a negative value if not */ int parse_stats(char *file_name, line_stats_t stats[]) { // TODO: Parts A and C return 0; } /* * Prints out statistics, with one per entry in stats. * * Inputs: * - stats: the array of line_stats_t * - num_lines: the number of entries in stats */ void print_results(line_stats_t stats[], int num_lines) { // TODO: Parts B and C } // Leave this here: it's a nice way to test your individual functions // without dividing this code into multiple files; any code between // this and the corresponding #endif will be deleted during testing, // so don't put anything here that you need! #ifndef __TESTING__ /* * Run the program! */ int main(int argc, char **argv) { // Get the filename char *file_name = NULL; if (argc == 2) { file_name = argv[1]; if (DEBUG) printf("The file to parse is: \"%s\"\n", file_name); } else { fprintf(stderr, "You need to provide the filename as a command-line argument. Good-bye!\n"); return 1; } // Allocate the array (assume at most MAX_LINES lines in the file) line_stats_t file_stats[MAX_LINES]; // Read the file and parse out stats int res = parse_stats(file_name, file_stats); if (res < 0) { fprintf(stderr, "An error occurred while parsing the file, returned: %d\n", res); return 1; } else { if (DEBUG) printf("Found %d lines.\n", res); } // Pretty-print out the results print_results(file_stats, res); // Return 0 to indicate success return 0; } #endif // __TESTING__