 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.

Slides:



Advertisements
Similar presentations
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
Advertisements

 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Chapter 7: User-Defined Functions II
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
1 Lecture13: Other C Topics 12/17/2012. Topics Variable-length argument lists Pointers to functions Command-line arguments Suffixes for integer and floating-point.
Chapter 14 - Advanced C Topics Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 20 - C Legacy Code Topics Outline 20.1 Introduction 20.2 Redirecting Input/Output on UNIX and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction.
Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 18 - C Legacy Code Topics Outline 18.1Introduction 18.2Redirecting Input/Output on UNIX and.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
C Program Design Supplements 主講人:虞台文. Content Who takes the return value of main() ? Using Command-Line arguments Variable-Length Argument Lists Program.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
File IO and command line input CSE 2451 Rong Shi.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
5.13 Recursion Recursive functions Functions that call themselves
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Programmazione I a.a. 2017/2018.
Chapter 2 - Introduction to C Programming
Chapter 14 - Advanced C Topics
Chapter 2 - Introduction to C Programming
File Input and Output.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Presentation transcript:

 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems 14.3Variable-Length Argument Lists 14.4Using Command-Line Arguments 14.5Notes on Compiling Multiple-Source-File Programs 14.8Suffixes for Integer and Floating-Point Constants 14.12The Unconditional Branch: goto

 2000 Prentice Hall, Inc. All rights reserved. 14.1Introduction Several advanced topics in this chapter Many capabilities are specific to operating systems (especially UNIX and/or DOS)

 2000 Prentice Hall, Inc. All rights reserved. 14.2Redirecting Input/Output on UNIX and DOS Systems Standard I/O - keyboard and screen –Redirect input and output Redirect symbol ( < ) –Operating system feature, NOT C++ feature –UNIX and DOS –$ or % represents command line Example: $ myProgram < input & –Rather than inputting values by hand, read them from a file Pipe command ( | ) –Output of one program becomes input of another $ firstProgram | secondProgram –Output of firstProgram goes to secondProgram

 2000 Prentice Hall, Inc. All rights reserved. 14.2Redirecting Input/Output on UNIX and DOS Systems (II) Redirect output ( > ) –Determines where output of a program goes –$ myProgram > myFile & Output goes into myFile (erases previous contents) Append output ( >> ) –Add output to end of file (preserve previous contents) –$ myOtherProgram >> myFile Output goes to the end of myFile

 2000 Prentice Hall, Inc. All rights reserved. 14.4Using Command-Line Arguments Pass arguments to main in DOS and UNIX int main( int argc, char *argv[] ) int argc - number of arguments passed char *argv[] - array of strings, has names of arguments in order ( argv[ 0 ] is first argument) Example: $ copy input output argc: 3 argv[ 0 ]: "copy" argv[ 1 ]: "input" argv[ 2 ]: "output"

 2000 Prentice Hall, Inc. All rights reserved. Outline 1. Initialize variables 2. Function calls ( fopen ) 2.1 Specify open type (read or write) 3. Copy file 1/* Fig. 14.3: fig14_03.c 2 Using command-line arguments */ 3#include 4 5int main( int argc, char *argv[] ) 6{6{ 7 FILE *inFilePtr, *outFilePtr; 8 int c; 9 10 if ( argc != 3 ) 11 printf( "Usage: copy infile outfile\n" ); 12 else 13 if ( ( inFilePtr = fopen( argv[ 1 ], "r" ) ) != NULL ) if ( ( outFilePtr = fopen( argv[ 2 ], "w" ) ) != NULL ) while ( ( c = fgetc( inFilePtr ) ) != EOF ) 18 fputc( c, outFilePtr ); else 21 printf( "File \"%s\" could not be opened\n", argv[ 2 ] ); else 24 printf( "File \"%s\" could not be opened\n", argv[ 1 ] ); return 0; 27} Notice argc and argv[] in main argv[1] is the second argument, and is being read. argv[2] is the third argument, and is being written to. Loop until End Of File. fgetc a character from inFilePtr and fputc it into outFilePtr.

 2000 Prentice Hall, Inc. All rights reserved. 14.5Notes on Compiling Multiple-Source- File Programs Programs with multiple source files –Function definition must be in one file (cannot be split up) –Global variables accessible to functions in same file Global variables must be defined in every file they are used –Example: Integer myGlobal defined in one file To use in another file: extern int myGlobal; extern - states that variable defined elsewhere (i.e., not in that file)

 2000 Prentice Hall, Inc. All rights reserved. 14.5Notes on Compiling Multiple-Source- File Programs (II) Programs with multiple source files (continued) –Function prototypes can be used in other files, extern not needed Have a prototype in each file that uses the function –Example: loading header files #include Contains prototypes of functions We do not know where definitions are

 2000 Prentice Hall, Inc. All rights reserved. 14.5Notes on Compiling Multiple-Source- File Programs (III) Keyword static –Variables can only be used in the file they are defined Programs with multiple source files –Tedious to compile everything if small changes made to one file –Can recompile only the changed files –Procedure varies on system UNIX: make utility

 2000 Prentice Hall, Inc. All rights reserved The Unconditional Branch: goto Unstructured programming –Use when performance crucial – break to exit loop instead of waiting until condition becomes false goto statement –Changes flow control to first statement after specified label –Label: identifier and colon (i.e. start: ) –Quick escape from deeply nested loop –goto start;

 2000 Prentice Hall, Inc. All rights reserved. Outline 1. Initialize variable 1.1 Labels 2. Loop 3. Print Program Output 1/* Fig. 14.9: fig14_09.c 2 Using goto */ 3#include 4 5int main() 6{6{ 7 int count = 1; 8 9 start: /* label */ 10 if ( count > 10 ) 11 goto end; printf( "%d ", count ); 14 ++count; 15 goto start; end: /* label */ 18 putchar( '\n' ); return 0; 21} Notice how start:, end: and goto are used