Hardware Reference
In-Depth Information
#include<stdio.h>
FILE *stream;
int main(argc, argv)
char *argv[];
int argc;
{
int c;
c=1;
stream = fopen(argv[1], "rb");
while(c != EOF)
{
c = getc(stream);
printf("%c", c);
}
fclose(stream);
return 0;
}
Assuming that the compiled program is named SPRINT.EXE, the command
line entered after the operating system prompt, would take the form:
SPRINT filename
To print a file called HELLO.DOC, the command would be:
SPRINT HELLO.DOC
More than one argument can be passed into main. The following Borland
C ++ 4.5 program is an example of a simple utility program for renaming files.
This program passes two arguments ( argc and argv ) into main:
/* Name:
rname.c
*/
/* Language: Borland C++ 4.5
*/
/* Output:
Menu routine
*/
/* Note:
Program runs in a DOS console window
*/
#include<io.h>
#include<stdio.h>
int main(argc, argv)
char *argv[];
int argc;
{
int result;
result = rename(argv[1], argv[2]);
if(result != 0)
printf("\nUnable to rename file!\n");
else
printf("\Rename successful!\n");
return 0;
}
Assuming that the program is called RNAME, the command line entered
after the operating system prompt, would take the form:
RNAME oldname newname
Search WWH ::




Custom Search