/******************************************************* * File: copyByChar.c * Author: Tim Finin * Date: 4/10/2005 * Modified by: Sue Evans * Date: 9/26/05 * Section: 01XX & 02XX * Email: bogar@cs.umbc.edu * * This program takes two command line arguments naming * files. It copies the contents of the first file to the * second a character at a time. * ********************************************************/ #include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { char c; FILE *ifp, *ofp; /* check for correct number of command line arguments */ if (argc != 3) { fprintf(stderr, "Usage: a.out inputfile outputfile\n"); exit(-1); } /* open input and output files */ ifp = fopen(argv[1], "r"); if (ifp == NULL) { fprintf(stderr, "Can't open %s for reading.\n", argv[1]); exit(-2); } ofp = fopen(argv[2], "w"); if (ofp == NULL) { fprintf(stderr,"Can't open %s for writing\n", argv[2]); exit(-3); } /* copy characters from input to output files until an EOF */ while(fscanf(ifp, "%c", &c) != EOF){ fprintf(ofp,"%c", c); } /* close files and return */ fclose(ifp); fclose(ofp); return(0); }
This program copies an input file into an output file character by character using fgetc().
/******************************************************* * File: eof.c * Author: Sue Evans * Date:11/25/05 * Section: 01XX & 02XX * Email: bogar@cs.umbc.edu * * This program takes two command line arguments naming * files. It copies the contents of the first file to the * second a character at a time. * ********************************************************/ #include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { int c; FILE *ifp, *ofp; /* check for correct number of command line arguments */ if (argc != 3) { fprintf(stderr, "Usage: a.out inputfile outputfile\n"); exit(-1); } /* open input and output files */ ifp = fopen(argv[1], "r"); if (ifp == NULL) { fprintf(stderr, "Can't open %s for reading.\n", argv[1]); exit(-2); } ofp = fopen(argv[2], "w"); if (ofp == NULL) { fprintf(stderr,"Can't open %s for writing\n", argv[2]); exit(-3); } /* copy characters from input to output files until an EOF */ while((c = fgetc(ifp)) != EOF){ fprintf(ofp,"%c", c); } /* close files and return */ fclose(ifp); fclose(ofp); return 0; }
This program copies an input file into an output file line by line.
/******************************************************* * File: copyByLine.c * Author: Tim Finin * Date: 4/10/2005 * Modified by: Sue Evans * Date: 9/26/05 * Section: 01XX & 02XX * Email: bogar@cs.umbc.edu * * This function takes two command line arguments naming * files. It copies the contents of the first file to the * second a line at a time. * ********************************************************/ #include <stdio.h> #include <stdlib.h> #define MAXLINELENGTH 999 int main (int argc, char *argv[]) { FILE *ifp, *ofp; char line[MAXLINELENGTH]; /* check for correct number of command line arguments */ if (argc !=3) { fprintf(stderr, "Usage: a.out inputfile outputfile\n"); exit(-1); } /* open input and output files */ ifp = fopen(argv[1], "r"); if (ifp == NULL) { fprintf(stderr, "Can't open %s for reading.\n", argv[1]); exit(-2); } ofp = fopen(argv[2], "w"); if (ofp == NULL) { fprintf(stderr,"Can't open %s for writing\n", argv[2]); exit(-3); } /* copy lines from input to output files until an EOF */ while (fgets(line, MAXLINELENGTH, ifp) != NULL ) { fputs(line, ofp); } /* close files and return */ fclose(ifp); fclose(ofp); return(0); }