Using Standard Libraries

Using Standard Libraries - Using Standard Libraries...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Using Standard Libraries INTRODUCTION TO C++ LIBRARIES PART 1 - <CASSERT> AND <CERRNO> We are going to spend some time looking at C++ libraries, starting with a couple of library utilities used for reporting errors. These are <cassert> and <cerrno>, and work the same in C++ as in C. We will be using the older <assert.h> and <errno.h>, because the newer names don't yet exist in many compilers. assert.h defines a macro assert() used to check assertions within a program. For example: #include <stdio.h> #include <assert.h> int main() { FILE* fp = fopen("test.txt", "r"); assert(fp != NULL); return 0; } If the argument to assert() is false (zero), the program terminates by calling the library function abort(), and gives a diagnostic as to the file and line of the error. In this example, an error like: Assertion failed: fp != NULL, file x2.c, line 8 Abnormal program termination comes out. Note that we could shorten the test to: assert(fp); identical to fp != NULL. assert() is useful for "should never happen" kinds of errors, or for quick prototyping. It's not really suitable as a primary tool for giving end-user error messages. Another error-reporting tool is <cerrno>. This has antecedents in the UNIX operating system, where a system call would return -1 on failure, and set a global variable "errno" to a number giving a more precise indication of what failed. An example of using this technique is: #include <stdio.h> #include <errno.h> #include <iostream.h> #include <string.h> int main() { errno = 0; FILE* fp = fopen("test.txt", "r"); if (fp == NULL)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
cout << strerror(errno) << endl; return 0; } errno is reset, and then an fopen() call made, which will ultimately invoke a system call open() to open a file. If fopen() returns NULL, errno can be queried to find out what exactly went wrong. strerror() is used to retrieve the text of the various error message codes represented by errno. In this example, the output is: No such file or directory This mechanism is useful in obtaining detail about errors, but you need to be careful to reset errno each time. Also, errno is not thread-safe. INTRODUCTION TO C++ LIBRARIES PART 2 - <STRING> C++ inherits C-style strings from C, where a sequence of characters is terminated with a null character and referenced via a char* pointer, and storage for dynamic strings must be explicitly managed. For example: #include <stdio.h> #include <string.h> int main() { char buf[25]; strcpy(buf, "testing"); printf("%s\n", buf); } This approach works pretty well and is efficient, but is quite low-level and prone to errors. A newer facility is C++-style strings. A simple example, that reads from standard input and writes each line to standard output, after reversing the characters in the line, looks like this: #include <iostream> #include <string> using namespace std; int main() { string instr; while (cin >> instr) { string outstr = ""; for (int i = instr.length() - 1; i >= 0; i--) outstr += instr[i]; cout << outstr << endl; } return 0; }
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue.

Page1 / 9

Using Standard Libraries - Using Standard Libraries...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online