170 likes | 251 Views
ECE230 Lectures Series. String Library and Stream I/O. Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu. An Unsolved Problem. a. b. Swap( ). b. a. Swap the value of two variables. void Swap(int *aa, int *bb); void main( ) {
E N D
ECE230 Lectures Series String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu
An Unsolved Problem a b Swap( ) b a • Swap the value of two variables void Swap(int *aa, int *bb); void main( ) { int a = 2, b = 3; Swap(&a, &b); cout << a << “ “ << b; } void Swap(int* aa, int* bb) { int *c; c = aa; aa = bb; bb = c; } Question: Will it work as we expect?
What shall we learn today? • Using string library • strcmp() • strcpy() • strlen() • atof() • Stream Manipulator • setw() • setprecision()
Library • A Library can be viewed as a toolbox • The implementations are hidden to users. • To make use of it, we only need to know the “interfaces”, i.e., the function prototypes • All the function prototypes can be found in the .h files • Once you use a function of a library, you need to include its .h file • Then, the compiler will know it is a function and reserve it for the linker when producing a .obj file. • Then, the linker will know which library to link with your program to produce a .exe file • There are many standard libraries you can use. You do not need to start from scratch. • You certainly can create libraries of your own.
strcmp() int strcmp( const char *string1, const char *string2 ); Return Value The return value indicates the lexicographic relation of string1 to string2. ValueRelationship of string1 to string2 < 0 string1 less than string2 = 0 string1 identical to string2 > 0 string1 greater than string2
#include <string> #define SIZE_CL_BUFF 500 void main() { char CL_Buffer[SIZE_CL_BUFF]; while(1){ cout << “>>” ; cin.getline(CL_Buffer, SIZE_CL_BUFF); if(strcmp(CL_Buffer, "quit")==0){ cout << "Thank you and Bye!" << endl; break; } else if(!strcmp(CL_Buffer, "who")){ cout << “I am Bill Gates.\n”; } else{ cout << “Hello world.\n”; } } }
strcpy() char *strcpy( char *strDestination, const char *strSource ); • Return Value • The function returns the destination string. • No return value is reserved to indicate an error. • Remarks • The strcpy function copies strSource, including the terminating null character, to the location specified by strDestination. • No overflow checking is performed. • The behavior of strcpy is undefined if the source and destination strings overlap.
#include <string.h> void main( void ) { char string[80]; strcpy( string, "Hello world!" ); cout << string << endl; char mystr[] = “ECE230”; cout << strcpy(string, mystr) << endl; // dangerous! char temp[5]; strcpy(temp, mystr); } • It is not a good idea to use a longer array for the destination • Since it may waste memories • What would be a good solution?
strlen() size_t strlen( const char *string ); Return Value It returns the number of characters in string, excluding the terminal NULL (i.e., the ‘\0’ character, i.e., 0). Remarks It returns the number of characters in string, not including the terminating null character.
#include <string.h> void main( void ) { char string[80]; strcpy( string, "Hello world!" ); cout << string << endl; char mystr[] = “ECE230”; cout << strcpy(string, mystr) << endl; // Let’s see this solution! char *temp; temp = new char [ strlen(mystr) + 1]; strcpy(temp, mystr); // once you are done with temp delete [] temp; }
atof() double atof( const char *string ); int atoi( const char *string); • Return Value • These returns the double, intvalue produced by interpreting the input characters as a number. • The return value is 0 (for atoi), or 0.0 (for atof) if the input cannot be converted to a value of that type.
Output: 2.03133e-15 -985 void main() { char *s; double x; int y; s = “2.031334342412E-15”; x = atof(s); cout << x << endl; s = “-985 pigs”; y = atoi(s); cout << y << endl; } • Woops! • How can I get the precision for my floating points?
Stream Manipulator • stream manipulator capabilities: • setting field widths • setting precisions • setting the fill character in fields • flushing streams • etc.
setprecision() • precision • member function • sets number of digits to the right of decimal point cout.precision(2); • cout.precision()returns current precision setting • setprecision • parameterized stream manipulator • Like all parameterized stream manipulators, <iomanip> required • specify precision: cout << setprecision(2) << x; • For both methods, changes last until a different value is set
void main() { char *s; s = “2.031334364”; for(int k=1; k<=10; k++){ cout << setprecision(k) << atof(s) << endl; } } 2 2 2.03 2.031 2.0313 2.03133 2.031334 2.0313343 2.03133436 2.031334364
Setting field widths • ios width member function • sets field width (number of character positions, a value should be output or number of characters that should be input) • returns previous width • if values processed are smaller than width, fill characters inserted as padding • values are not truncated - full number printed • cin.width(5); • setw stream manipulator cin >> setw(5) >> string; • Remember to reserve one space for the null character
void main() { double x[] = {2.15, 3.1415, 3.9}; double y[] = {3.1, 4.11, 5.944}; for(int k=0; k<3; k++) cout << setw(10) << x[k]; cout << endl; for(k=0;k<3;k++) cout << setw(10) << y[k]; cout << endl; } ______2.15____3.1415_______3.9 _______3.1______4.11_____5.944 (note, _ means a space)