|
|
|
|
|
|
|
cout << "** Can't open input file **" << endl;
return 1;
}
.
.
. |
|
|
|
|
|
|
|
|
From now on, our end-of-chapter case studies use this technique of reading a file name at run time. |
|
|
|
|
|
|
|
|
Through the header file string.h, the C++ standard library provides a large assortment of string operations that people have found to be useful.In this section, we discuss three of these library functions: strlen, which returns the length of a string; strcmp, which compares two strings using the relations less-than, equal, and greater-than; and strcpy, which copies one string to another. Here is a summary of strlen, strcmp, and strcpy: |
|
|
|
|
| Header File | Function | Function Value | Effect | | <string.h> | strlen(str) | Integer length of str (excluding '\0') | Computes length of str. | | <string.h> | strcmp(str1, str2) | An integer <0, if str1 < str2 The integer 0, if str1 = str2 An integer > 0, if str1 > str2 | Compares str1 and str2. | | <string.h> | strcpy(toStr, fromStr) | Base address of toStr (usually ignored) | Copies fromStr (including '\0') to toStr, overwriting what was there; toStr must be large enough to hold the result. |
|
|
|
|
|
|
The strlen function is similar to the StrLength function we wrote earlier. It returns the number of characters in a string prior to the terminating '\0'. Here's an example of a call to the function: |
|
|
|
|
|
|
|
|
#include <string.h>
.
.
.
char subject[] = "Computer Science";
cout << strlen(subject); // Prints 16 |
|
|
|
|
|