Skip to content

Instantly share code, notes, and snippets.

Created September 25, 2018 17:17
What would you like to do?
write simple data to a file
create a program that creates a file
and makes a copy of it. Command line usage:
./test <infile> <outfile>
#include <stdio.h>
int main(int argc, char* argv[])
//~*~*~*~*VALIDATE USER INPUTS*~*~*~*~//
// validate command line arguments
if (argc != 3)
printf("error: proper usage: ./test <infile> <outfile>\n");
return 1;
printf("%s %s %s\n", argv[0], argv[1], argv[2]);
// validate infile
char* input_file_name = argv[1];
FILE* input_file_pointer = fopen(input_file_name,"r");
if (input_file_pointer == NULL)
printf("Could not open %s\n", input_file_name);
return 2;
//~*~*~*~*WRITE TO OUTPUT FILE*~*~*~*~//
char* output_file_name = argv[2];
FILE* output_file_pointer = fopen(output_file_name, "w");
char c;
// we need to break once the null terminator is
// collected in our variable which is why we
// have our break statement after assigning
// our variable c
// in other words we don't want to write the last
// byte to our outfile
c = fgetc(input_file_pointer);
if (feof(input_file_pointer))
fputc(c, output_file_pointer);
printf("%c", c);
//~*~*~*~*CLOSE OUT FILE POINTERS*~*~*~*~//
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment