//: C04:Facets.cpp
//{-bor}
//{-g++}
#include <iostream>
#include <locale>
#include <string>
using namespace std;
int main() {
// Change to French/France
locale loc("french");
cout.imbue(loc);
string currency =
use_facet<moneypunct<char> >(loc).curr_symbol();
char point =
use_facet<moneypunct<char> >(loc).decimal_point();
cout << "I made " << currency << 12.34 << " today!"
<< endl;
} ///:~
The output shows the French currency symbol and decimal separator:
I made Ç12,34 today!
You can also define your own facets to construct customized locales.[47] Be aware that the overhead for locales is considerable. In fact, some library vendors provide different "flavors" of the standard C++ library to accommodate environments that have limited space.[48]
Summary
This chapter has given you a fairly thorough introduction to the iostream class library. In all likelihood, it is all you need to create programs using iostreams. However, be aware that some additional features in iostreams are not used often, but you can discover them by looking at the iostream header files and by reading your compiler’s documentation on iostreams or the references mentioned in this chapter and in the book’s preface.
Exercises
1. Open a file by creating an ifstream object. Make an ostringstream object and read the entire contents into the ostringstream using the rdbuf( ) member function. Extract a string copy of the underlying buffer and capitalize every character in the file using the Standard C toupper( ) macro defined in <cctype>. Write the result out to a new file.</#><#TIC2V2_CHAPTER5_I190>
24. Create a program that opens a file (the first argument on the command line) and searches it for any one of a set of words (the remaining arguments on the command line). Read the input a line at a time, and write out the lines (with line numbers) that match to the new file.</#><#TIC2V2_CHAPTER5_I191>
25. Write a program that adds a copyright notice to the beginning of all source-code files indicated by the program’s command-line arguments.</#><#TIC2V2_CHAPTER5_I192>
26. Use your favorite text-searching program (grep, for example) to output the names (only) of all the files that contain a particular pattern. Redirect the output into a file. Write a program that uses the contents of that file to generate a batch file that invokes your editor on each of the files found by the search program.
27. We know that setw( ) allows for a minimum of characters read in, but what if you wanted to read a maximum? Write an effector that allows the user to specify a maximum number of characters to extract. Have your effector also work for output, in such a way that output fields are truncated, if necessary, to stay within width limits.
28. Demonstrate to yourself that if the fail or bad bit is set, and you subsequently turn on stream exceptions, that the stream will immediately throw an exception.
29. String streams accommodate easy conversions, but they come with a price. Write a program that races atoi( ) against the stringstream conversion system to see the effect of the overhead involved with stringstream.
30. Make a Person struct with fields such as name, age, address, etc. Make the string fields fixed-size arrays. The social security number will be the key for each record. Implement the following Database class.
class DataBase {
public:
// Find where a record is on disk
size_t query(size_t ssn);
// Return the person at rn (record number)
Person retrieve(size_t rn);
// Record a record on disk
void add(const Person& p);
};
Write some Person records to disk (do not keep them all in memory). When the user requests a record, read it off the disk and return it. The I/O operations in the DataBase class use read( ) and write( ) to process all Person records.
31. Write an operator<< inserter for the Person struct that can be used to display records in a format easily read. Practice writing it out to file.
32. Suppose your database for your Person structs was lost but that you have the file you wrote from the previous exercise. Recreate your database using this file. Be sure to use error checking.
33. Write size_t(-1) (the largest unsigned int on your platform) to a text file 1,000,000 times. Repeat, but write to a binary file. Compare the two files for size, and see how much room is saved using the binary format. (You may first want to try to calculate how much will be saved on your platform.)
34. Found out the maximum number of digits of precision your implementation of iostreams will print by repeatedly increasing the value of the argument to precision( ) when printing a transcendental number such as sqrt(2.0).
35. Write a program that reads real numbers from a file and prints their sum, average, minimum, and maximum.
36. Determine the output of the following program before it is executed.
//: C04:Exercise16.cpp
#include <fstream>
#include <iostream>
#include <sstream>
#include "../require.h"
using namespace std;
#define d(a) cout << #a " ==\t" << a << endl;
void tellPointers(fstream& s) {
d(s.tellp());
d(s.tellg());
cout << endl;
}
void tellPointers(stringstream& s) {
d(s.tellp());
d(s.tellg());
cout << endl;
}
int main() {
fstream in("Exercise16.cpp");
assure(in, "Exercise16.cpp");
in.seekg(10);
tellPointers(in);
in.seekp(20);
tellPointers(in);
stringstream memStream("Here is a sentence.");
memStream.seekg(10);
tellPointers(memStream);
memStream.seekp(5);
tellPointers(memStream);
} ///:~
37. Suppose you are given line-oriented data in a file formatted as follows:
Australia
48
See, for example, Dinkumware’s Abridged library at www.dinkumware.com. This library omits locale support. and exception support is optional.