Академический Документы
Профессиональный Документы
Культура Документы
Input to program now comes from file input, not the keyboard
2003 Prentice Hall, Inc. All rights reserved.
20.2 Redirecting Input/Output on UNIX and DOS Systems Append output (>>)
Output of program appends to end of file $ myProgram >> myFile
Output goes to end of myFile
Function format
Include <cstdarg> Use ellipsis () at end of parameter list
Must be last item in parameter list Must be one named parameter before ellipsis
double myFunction(int i, );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// Fig. 20.2: fig20_02.cpp // Using variable-length argument lists. #include <iostream> using std::cout; using std::endl; using std::ios; #include <iomanip> using using using using std::setw; std::setprecision; std::setiosflags; std::fixed;
Outline
fig20_02.cpp (1 of 3)
#include <cstdarg> double average( int, ... ); int main() { double double1 double double2 double double3 double double4
Note use of ellipsis in the prototype, and one defined argument before it.
= = = =
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
cout << << << << << << << << << << << <<
fixed << setprecision( 1 ) << "double1 = " double1 << "\ndouble2 = " << double2 << "\ndouble3 = " double3 << "\ndouble4 = " << double4 << endl setprecision( 3 ) "\nThe average of double1 and double2 is " average( 2, double1, double2 ) "\nThe average of double1, double2, and double3 is " average( 3, double1, double2, double3 ) "\nThe average of double1, double2, double3" " and double4 is " average( 4, double1, double2, double3, double4 ) endl;
10
Outline
fig20_02.cpp (2 of 3)
Call function with a variable number of arguments (passing the number of arguments as a parameter).
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
// calculate average Create a va_list object and double average( int count, ... ) { call macro va_start. double total = 0; count is the parameter va_list list; // for storing information needed by va_start
11
Outline
fig20_02.cpp (3 of 3)
va_start( list, count ); // process variable length argument list for ( int i = 1; i <= count; i++ ) total += va_arg( list, double ); // end the va_start va_end( list ); return total / count; } // end function average
= = = =
12
Outline
fig20_02.cpp output (1 of 1)
The average of double1 and double2 is 30.000 The average of double1, double2, and double3 is 20.567 The average of double1, double2, double3 and double4 is 17.975
13
int argc
Number of arguments
char *argv[]
Array of strings that contains command-line arguments
14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// Fig. 20.3: fig20_03.cpp // Using command-line arguments #include <iostream> using std::cout; using std::endl; using std::ios; #include <fstream> using std::ifstream; using std::ofstream;
15
Outline
fig20_03.cpp (1 of 2)
int main( int argc, char *argv[] ) { // check number of command-line arguments if ( argc != 3 ) argv[1] is the cout << "Usage: copyFile infile_name outfile_name" << endl; else { ifstream inFile( argv[ 1 ], ios::in );
input file --
// input file could not be opened if ( !inFile ) { cout << argv[ 1 ] << " could not be opened" << endl; return -1;
} // end if
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
16
ofstream outFile( argv[ 2 ], ios::out ); // output file could not be opened if ( !outFile ) { cout << argv[ 2 ] << " could not be opened" inFile.close(); return -2;
Outline
argv[2] is the output file -open for writing. fig20_03.cpp (2 of 2) << endl;
} // end if
char c = inFile.get(); // read while ( inFile ) { outFile.put( c ); c = inFile.get(); } // end while // end else
Read a character from firstinFile character , and write to outFile. Loop stops when EOF reached.
return 0;
// end main
17
20.5 Notes on Compiling Multiple-SourceFile Programs Program with multiple source files
Function definition must be entirely in one file
Cannot be split up into multiple files
Example
int myGlobal; (defined in file1) extern int myGlobal; (appears in file2)
18
Prototype indicates function defined later in same file, or in another file Example: loading header files
#include <cstring> Contains prototypes of functions We do not need to know where definitions are
19
Used with utility functions called only in one file For functions
If defined before used, include static in definition Otherwise, use with prototype
Makefiles
make - utility to aid compilation and linking Saves effort of constantly recompiling for minor changes
2003 Prentice Hall, Inc. All rights reserved.
20
21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Fig. 20.4: fig20_04.cpp // Using the exit and atexit functions #include <iostream> using std::cout; using std::endl; using std::cin; #include <cstdlib> void print(); int main() { atexit( print );
22
Outline
fig20_04.cpp (1 of 2)
//
Register print to be called when the program terminates. print must return void and take no function arguments. register print
print will be called if theexit" cout << "Enter 1 to terminate program with function << "\nEnter 2 to terminate program program endsnormally\n"; successfully.
int answer; cin >> answer;
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
// exit if answer is 1 if ( answer == 1 ) { cout << "\nTerminating program with function exit\n"; exit( EXIT_SUCCESS ); } // end if
23
Outline
fig20_04.cpp (2 of 2)
cout << "\nTerminating program by reaching the end of main" << endl; return 0; } // end main
// display message before termination void print() { cout << "Executing function print at program termination\n" << "Program terminated" << endl; } // end function print
Enter 1 to terminate program with function exit Enter 2 to terminate program normally 2 Terminating program by reaching the end of main Executing function print at program termination Program terminated Enter 1 to terminate program with function exit Enter 2 to terminate program normally 1 Terminating program with function exit Executing function print at program termination Program terminated
24
Outline
fig20_04.cpp output (1 of 1)
25
26
20.8 Suffixes for Integer and Floating-Point Constants C++ has suffixes for constants
Integer suffixes
u or U (unsigned) l or L (long) ul or UL (unsigned long)
Without suffix, uses smallest type that can hold number Examples: 174u, 1322L, 7364ul
27
28
29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Fig. 20.6: fig20_06.cpp // Using signal handling #include <iostream> using std::cout; using std::cin; using std::endl; #include <iomanip> using std::setw; #include <csignal> #include <cstdlib> #include <ctime> void signalHandler( int ); int main() { signal( SIGINT, signalHandler ); srand( time( 0 ) );
30
Outline
fig20_06.cpp (1 of 3)
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
// create and output random numbers for ( int i = 1; i <= 100; i++ ) { int x = 1 + rand() % 50; // raise SIGINT when x is 25 if ( x == 25 ) raise( SIGINT ); cout << setw( 4 ) << i;
31
Outline
Note call to function raise.
fig20_06.cpp (2 of 3)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
// handles signal void signalHandler( int signalValue ) { cout << "\nInterrupt signal (" << signalValue << ") received.\n" << "Do you wish to continue (1 = yes or 2 = no)? "; int response;
32
Outline
fig20_06.cpp (3 of 3)
// call signal and pass it SIGINT and address of signalHandler signal( SIGINT, signalHandler );
} // end function signalHandler
10 20 30 40 50 60 70 80 90
33
Outline
fig20_06.cpp output (1 of 1)
= no)? 1
34
20.10 Dynamic Memory Allocation with calloc and realloc Dynamic memory allocation
Can create dynamic arrays
Function calloc
void *calloc(size_t nelmt, size_t size)
nelmt - number of elements in array size - size of each element
35
20.10 Dynamic Memory Allocation with calloc and realloc Function realloc
Resizes dynamic object
Data not modified if size increased If shrunk, beginning the same
36
goto statement
goto label; Program jumps to first statement after label Label is an identifier and colon (start:)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Fig. 20.7: fig20_07.cpp // Using goto. #include <iostream> using std::cout; using std::endl; #include <iomanip>
37
Outline
fig20_07.cpp (1 of 2)
cout << setw( 2 ) << left << count; Note the ++count;
// goto start on line 17 goto start;
28 29 30 31 32 33 34 35
38
end: // label
Outline
fig20_07.cpp (2 of 2) fig20_07.cpp output (1 of 1)
10
39
Conserves storage Only the last data member defined can be accessed Declaration same as class or struct
union Number { int x; float y; } ; Union myObject;
40
41
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Fig. 20.8: fig20_08.cpp // An example of a union. #include <iostream> using std::cout; using std::endl; // define union Number union Number { int integer1; double double1; }; // end union Number
42
Outline
fig20_08.cpp (1 of 2)
Create a named union with two data members. They share the same memory.
"Put a value in the integer member\n" "and print both members.\nint: " value.integer1 << "\ndouble: " << value.double1 endl;
The program output is implementation dependent, but will show how ints and doubles are represented differently.
2003 Prentice Hall, Inc.
All rights reserved.
26 27 28 29 30 31 32 33 34 35
43
Outline
fig20_08.cpp (2 of 2) fig20_08.cpp output (1 of 1)
"Put a value in the floating member\n" "and print both members.\nint: " value.integer1 << "\ndouble: " << value.double1 endl;
return 0;
// end main
Put a value in the integer member and print both members. int: 100 double: -9.25596e+061 Put a value in the floating member and print both members. int: 0 double: 100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Fig. 20.9: fig20_09.cpp // Using an anonymous union. #include <iostream> using std::cout; using std::endl; int main() { Create an anonymous union. // declare an anonymous union The data members can be // members integer1, double1 and charPtr share the same space accessed without using a union { union name. int integer1; double double1; char *charPtr; }; // end anonymous union
44
Outline
fig20_09.cpp (1 of 2)
// declare local variables int integer2 = 1; double double2 = 3.3; char *char2Ptr = "Anonymous";
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// assign value to each union member // successively and print each cout << integer2 << ' '; integer1 = 2; cout << integer1 << endl; cout << double2 << ' '; double1 = 4.4; cout << double1 << endl; cout << char2Ptr << ' '; charPtr = "union"; cout << charPtr << endl; return 0; } // end main
45
Outline
fig20_09.cpp (2 of 2) fig20_09.cpp output (1 of 1)
46
20.13 Linkage Specifications Can call compiled C functions from C++ program
However, C does not encode function names like C++ Leads to problems linking
Linkage specifications
To link properly, tell compiler that function compiled in C For single functions
extern "C" function prototype