/* line_counter.c Written by Tanya Amert for Fall 2024 Prints out the count of lines in a file. Compile as usual: gcc -Wall -Werror -g -o line_counter line_counter.c Run with one command-line argument, specifying a valid filename: ./line_counter myfile.txt */ #include // Method declarations void print_usage_statement(const char *program_name); int get_line_count(const char *file_name); // Counts the number of lines in a file specified // as a command-line argument; prints the results to // standard output int main(int argc, char *argv[]) { // Check that we have enough command-line arguments if (argc != 2) { print_usage_statement(argv[0]); return 1; // anything other than 0 indicates failure } // Parse the command-line argument for the filename const char *input_file_name = argv[1]; // Do the work int line_count = get_line_count(input_file_name); if (line_count < 0) { fprintf(stderr, "Trouble counting the lines in %s\n", input_file_name); return 1; // another failure case } printf("Number of lines: %d (0x%x)\n", line_count, line_count); return 0; } // Prints information for how to use this program. void print_usage_statement(const char *program_name) { fprintf(stderr, "Usage: %s inputfile\n", program_name); fprintf(stderr, " prints to standard output the number of lines\n"); fprintf(stderr, " (as a decimal integer) in the specified file\n"); } // Returns the number of lines in the specified file, or -1 if an error // occurs. Errors include failure to open or read from the file. // // Assumes the last line does not end in '\n'. int get_line_count(const char *file_name) { // Open the file for reading FILE *input_file = fopen(file_name, "r"); if (input_file == NULL) { return -1; } // Keep track of the current line number and how long it is int current_line = 0; // Loop through the file one char/byte at a time, counting lines as we go char ch = fgetc(input_file); while (ch != EOF) { // Check if we found a newline character if (ch == '\n') { // Get ready for the next line current_line++; } // Read the next character ch = fgetc(input_file); } // If ferror returns true, we hit EOF because of some issue, so fail out if (ferror(input_file)) { fclose(input_file); return -1; } // Clean up after ourselves fclose(input_file); // Return the line count +1 as the number of lines we saw return current_line + 1; }