// Write a C program which removes all of the non-printable // ASCII bytes (except for new lines) // from a file specified by the first command-line argument. // You may create temporary files // You can not assume file is small enough to be read completely into memory //What do we think will happen to emoji? //What do we think will happen if we remove the close files? //Can we use rename instead? #include #include //Strategy: //REad in file //abdcXabcbdXabcb <-Read this in //abdcabc <-print to a temp file //copy it back to the original one // Args: // argv[0] = ./file_revision // argv[1] = f_name int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Incorrect usage %s\n", argv[0]); return 1; } FILE *file_reader = fopen(argv[1], "r"); if (file_reader == NULL) { perror("file_reader"); return 1; } FILE *temp_writer = fopen("temp", "w"); if (temp_writer == NULL) { perror("temp_writer"); return 1; } // read in bytes from file_reader // write the bytes to the temp file int byte; while ((byte = fgetc(file_reader)) != EOF) { if(isprint(byte) || byte == '\n'){ fputc(byte, temp_writer); } } fclose(temp_writer); fclose(file_reader); rename("temp", argv[1]); /* // Copy it all back FILE *temp_reader = fopen("temp", "r"); if (temp_reader == NULL) { perror("temp_reader"); return 1; } FILE *file_writer = fopen(argv[1], "w"); if (file_writer == NULL) { perror("file_writer"); return 1; } while ((byte = fgetc(temp_reader)) != EOF) { fputc(byte, file_writer); } fclose(temp_reader); fclose(file_writer); */ return 0; }