/** * A simple copy function for text files * * @author gtowell * Created: Mar 2021 * **/ #include #ifndef NULL #define NULL (void *) 0 #endif #define LINE_LEN 256 int main(int argc, char const *argv[]) { if (argc < 3) { printf("Usage: xxx existing_file_name name_of_copy"); return 0; } FILE *fInput = fopen(argv[1], "r"); if (NULL == fInput) { fprintf(stderr, "Failed to open %s for reading ... terminating\n", argv[1]); return 1; } FILE *fOutput = fopen(argv[2], "w"); if (NULL == fOutput) { fprintf(stderr, "Failed to open %s for output ... terminating\n", argv[2]); return 1; } char line[LINE_LEN]; // this loop actually does the copying while (NULL != fgets(line, LINE_LEN, fInput)) { fprintf(fOutput, "%s", line); } // always close what was opened fclose(fInput); fclose(fOutput); return 0; }