Skip to content

Instantly share code, notes, and snippets.

@sojohnnysaid
Created September 25, 2018 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sojohnnysaid/aaa57a64af068a470e87ee8a189881cf to your computer and use it in GitHub Desktop.
Save sojohnnysaid/aaa57a64af068a470e87ee8a189881cf to your computer and use it in GitHub Desktop.
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;
while(1)
{
// 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))
{
break;
}
fputc(c, output_file_pointer);
printf("%c", c);
}
printf("\n");
//---------------------------------------//
//~*~*~*~*CLOSE OUT FILE POINTERS*~*~*~*~//
//---------------------------------------//
fclose(input_file_pointer);
fclose(output_file_pointer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment