Command Line Parameters

Slides:



Advertisements
Similar presentations
CMPE13 Cyrus Bazeghi Hands on with Functions and IO.
Advertisements

Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
Advanced Programming in the UNIX Environment Hop Lee.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Some Example C Programs. These programs show how to use the exec function.
File Handling Spring 2013Programming and Data Structure1.
Command Line Arguments plus Variable-Length Arrays Systems Programming.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
USER DEFINED FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
 Dynamic Memory Allocation  Linked Lists  void main() {{ ◦ FILE *file; ◦ char file_name[] = “file.txt”; ◦ if ((file = fopen(file_name, "w")) ==
LINKED LISTS.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
C Primer.
A bit of C programming Lecture 3 Uli Raich.
Characters and Strings
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
CISC105 – General Computer Science
Protection of System Resources
Command Line Arguments
Command line arguments
Agenda Make Utility Command Line Arguments in Unix
Command Line Arguments
Functions and Structured Programming
Understand argc and argv
C Language By Sra Sontisirikit
Day 02 Introduction to C.
Structure of C Programs
Command Line Arguments
CSE 351 Section 1: HW 0 + Intro to C
CSC215 Lecture Input and Output.
Command-Line Arguments
KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY
Programmazione I a.a. 2017/2018.
Programming in C Input / Output.
More Examples of argc and argv
Command-line Arguments
Computer Science 210 Computer Organization
Programming in C Input / Output.
Yung-Hsiang Lu Purdue University
File I/O We are used to reading from and writing to the terminal:
Command Line Arguments
C Stuff CS 2308.
Command-line arguments
2.1 Processes process = abstraction of a running program
CSE1320 Strings Dr. Sajib Datta
sscanf()- string scan function
Programming and Data Structure
File Handling.
Programming in C Miscellaneous Topics.
Programming in C Miscellaneous Topics.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2015.
Command-line arguments
CNT4704: Analysis of Computer Communication Network Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Fall 2011.
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
Quick Review EECS May 2019.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2016.
Characters and Strings
15213 C Primer 17 September 2002.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2013.
I/O CS580U - Fall 2018.
ENERGY 211 / CME 211 Lecture 29 December 3, 2008.
Professor Jodi Neely-Ritz University of Florida
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

Command Line Parameters

Passing Unix parameters to C (1) Often a user wants to pass parameters into the program from the UNIX prompt obelix[3] > progname arg1 arg2 arg3 This is accomplished in C using argc and argv For example: int main (int argc, char *argv[ ] ) { /* Statements go here */ } Call this program from Unix obelix [4] > progname arg1 "second arg" arg3 array of strings (char *) Number of arguments

Passing Unix parameters to C (2) #include <stdio.h> int main(int argc, char *argv[]) { int count; printf ("Program name: %s\n", argv [0]); if (argc > 1) { for (count=1; count<argc; count++) printf ("Argument %d: %s\n",count,argv[count]); } else puts ("No command line arguments entered."); return 0;

Passing Unix parameters to C (3) Suppose we compiled the previous program to the executable file myargs obelix[5] > myargs first "second arg" 3 4 > myargs.out myargs.out contains the following lines: Program name: myargs Argument 1: first Argument 2: second arg Argument 3: 3 Argument 4: 4

Passing Unix arguments to C (4) /* show file */ #include <stdio.h> int main(int argc, char *argv[ ]) { FILE *fp; int k; if(argc !=2) { printf("Usage: %s filename\n", argv[0]); return 0; } if((fp=fopen(argv[1], "r"))==NULL){ printf("Cannot open file!\n"); return 1; while((k=fgetc(fp))!=EOF ) fputc(k, stdout); fclose(fp); return 0; } Generally a main function of a C program for Unix checks whether the arguments are valid and prints simple help information.