Sunday, January 12, 2014

open the first command line argument for reading and the second command line argument for writing in C


University of Illinois Chicago
CS211 – Programming Practicum
Fall 2013
Programming Project 1
Due: Thursday 8/29/2012 at 11:59pm
A simple Copy command
For this lab, you are to write a simple version of the Linux/Unix copy command: cp.
This is to be written in the C programming language.
The command is to take two command line arguments:
1. the original filename
2. the destination filename
The program should first verify that there are two command line arguments. These arguments are
to be used as names for file input/output.
The program is to open the first command line argument for reading and the second command
line argument for writing. Then it should read the contents of the first file until it reaches the end
of the file and place those contents into the second file.
Both files need to be closed before the program terminates.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE* inFile;
FILE* outFile;
char c[100];
if (argc!=3)
{
printf("Usage: copyfile inFile outFile");
return 0;
}
inFile  = fopen (argv[1], "r");
outFile = fopen (argv[2], "w");
if (inFile == NULL)
{
printf("Error opening input file");
}
else
{
while ((fgets(c, 100, inFile)) != NULL) {
fputs(c, outFile);
}
}
fclose (inFile);
fclose (outFile);
return 0;
}

No comments:

Post a Comment