Command Line arguments - PowerPoint PPT Presentation

About This Presentation
Title:

Command Line arguments

Description:

Then you can declare using the label. Examples: Declare a new type. typedef char * String; ... Data goes to an operating system buffer. When does the data write? ... – PowerPoint PPT presentation

Number of Views:350
Avg rating:3.0/5.0
Slides: 18
Provided by: set134
Learn more at: http://cs.sou.edu
Category:

less

Transcript and Presenter's Notes

Title: Command Line arguments


1
Command Line arguments
  • Main function int main(argc, char argv)
  • argc is the count of command line arguments
  • argv is an array of strings
  • argv0 is the name of the program
  • argv1 is the first argument from the command
    line
  • argvk is the kth argument from the command
    line
  • Command line example gtprog 123 abc
  • argc 3
  • argv0 "prog", argv1 "123", argv2
    "abc"
  • If you expect two argumentsif (argc!3)
    printf("usage prog ltarg1gt ltarg2gt) exit(1)

2
Inputting Problem
  • scanf User can crash the program by typing too
    much
  • fgets User cannot crash the program
  • Example
  • includeltstdio.hgt
  • int main(void)
  • char input101
  • char result
  • printf("Enter some text\n")
  • result fgets(input, sizeof(input), stdin)
  • if (result ! NULL) printf("You entered
    s", result) else printf("Error processing
    fgets()\n")
  • return 0

3
typedef
  • typedef assigns a label to a declaration
  • Syntax typedef ltsome typegt label
  • Then you can declare using the label
  • Examples
  • Declare a new typetypedef char Stringtypedef
    int Boolean
  • Declare an instance of that typevoid
    myFunc(String data, Boolean flag)

4
struct
  • struct is a collection of data types
  • Examplestruct int age char name100
    1 float salary
  • Useful to create a new complex typetypedef
    struct int age char name1001 float
    salary Person
  • Instantiate Person bill

Note A complex type is like Java class, but
without methods
5
Accessing struct data
  • Declare Person person100
  • Access fields JLJprintf("His name is s\n",
    person0.name)for (int i0 ilt100 i)
    printf("s is d years old with salary f\n"
    , personi.name , personi.age
    , personi.salary)

6
Using pointers for functions
  • void myFunc(const Person first,
  • Person second)
  • second-gtname first.name
  • second-gtage first.age
  • second-gtsalary first.salary
  • Note The arrow indirectly addresses access data
    in structs
  • Note Pass by value copies the entire structure
    to the stack. This can be inefficient, so most
    programmers always use pointers in functions

7
Reading a text file
  • include ltstdio.hgt
  • int main(void)
  • char data256
  • FILE file
  • file fopen("someFile.txt","r") if
    (fileNULL) return 2
  • while (!feof(file))
  • int cSizesizeof(char) // normally 1
  • int dSizesizeof(data) // 256 cSize
  • int size dSize/cSize // Array byte size
  • fgets(data, size,file)
  • printf("s",data)
  • fclose(file)
  • return 0

8
Writing to a text file
  • include ltstdio.hgt
  • int main()
  • FILE file char data2561
  • fgets(data, sizeof(data), stdin)
  • file fopen("someFile.txt", "w")
    if(fileNULL) fprintf(stderr, "Can't create
    output file\n") return 2 fprintf(file,
    "s\n", data)
  • fclose(file)
  • return 0

Question Why not use printf instead of fprintf
with stderr
9
fscanf and fgets to read files
  • fscanf(File fp, const char temp, args )
  • Works just like scanf but is insecure
  • Stops on white space
  • No protection for buffer size
  • Better approach
  • Use fgets reads till newline or n-1 characters
    read
  • Automatically puts the null at the end of the
    string
  • Returns null if error or end of file
  • If you have to parse, use sscanf after the data
    is in memory

10
sscanf
  • Examplechar str "30 20 10"int a, b,
    cif (sscanf(str, "d d d", a, b, c) ! 3)
    fprintf(stderr, "What happened?")
  • sscanf is a very useful tool for parsing input or
    data read from files
  • Note the strstr function is useful for finding
    the first occurrence of a little string in a big
    one. See lab 10.char strstr (const char
    haystack, const char needle)

11
Buffered Output
  • Data does NOT go dirctly to a file
  • Why? It is more efficient for the operating
    system to decide when to write data.
  • What happens? Data goes to an operating system
    buffer
  • When does the data write? When the buffer is full
    or manually flushed
  • How does it manually get flushed? Use the call
    fflush(file)
  • What about stdout? call fflush(stdout)

12
Final Notes
  • Portability
  • Operating Systems end lines differently
  • C always uses '\n' to end a line
  • stdio functions translate for the OS in question
  • EOF stdio functions use this constant
  • include lt stdio.hgt
  • void main()
  • int c, nc 0
  • while ( (c getchar()) ! EOF ) nc
  • printf("Characters in file d\n",nc)
  • GDB The set args command is useful before
    running the program. It sets the command line
    arguments as if you typed them from the command
    line

13
Make
  • Utility to build systems composed of many source
    and data files
  • Purpose
  • To minimize management errors of incorrect builds
    made after a minor change
  • To only compile and build those things that were
    changed since the previous build
  • To create source for creating systems geared for
    certain configurations and locals

14
Installations
  • Typical installations of applications from source
  • Obtain tar.gz bundle and uncompress
  • Execute ./configure script to set platform-based
    parameters. The script may launch a set of C
    based programs to inquire about the linux
    configuration
  • Execute make to compile and create executables
  • The linux command to execute make
  • make -f custom make file buildType
  • Note If no custom make file, the command looks
    for a file named makefile
  • Note buildType tells the make script the kind of
    build desired.

15
Make Command Script Example
  • all hello
  • hello main.o factorial.o hello.o
  • g main.o factorial.o hello.o -o hello
  • main.o main.cpp
  • g -c main.cpp
  • factorial.o factorial.cpp
  • g -c factorial.cpp
  • hello.o hello.cpp
  • g -c hello.cpp
  • clean
  • rm -rf o hello

Syntax target dependencies tab
system commandCreate target if the dependencies
are met
16
Using Make Variables
  • CCg
  • CFLAGS-c -Wall
  • all hello
  • hello main.o factorial.o hello.o
  • (CC) main.o factorial.o hello.o -o hello
  • main.o main.cpp
  • (CC) (CFLAGS) main.cpp
  • factorial.o factorial.cpp
  • (CC) (CFLAGS) factorial.cpp
  • hello.o hello.cpp
  • (CC) (CFLAGS) hello.cpp
  • clean
  • rm -rf o hello

Use g for the compiler.
Use G option flags compile and all warnings
17
A More Complex Example
  • CCg
  • CFLAGS-c -Wall
  • LDFLAGS
  • SOURCESmain.cpp hello.cpp factorial.cpp
    OBJECTS(SOURCES.cpp.o)
  • EXECUTABLEhello
  • all (SOURCES) (EXECUTABLE)
  • (EXECUTABLE) (OBJECTS)
  • (CC) (LDFLAGS) (OBJECTS) -o _at_
  • .cpp.o
  • (CC) (CFLAGS) lt -o _at_
  • http//www.gnu.org/software/make/manual/make.pdf
Write a Comment
User Comments (0)
About PowerShow.com