The real number 678.0 is printed: 678
+ When ios::fixed is on and the ios::showpoint flag is on, the real number is printed in decimal form, the number of fractional digits (after the decimal point) printed is exactly equal to n precision.
For example, if the precision n = 4 then:
The real number -87.1500 is printed: -87.1500
Maybe you are interested!
-
Object Oriented Programming - 30 -
Object Oriented Programming - 23 -
Query Processing and Optimization in Distributed Object-Oriented Databases - 17 -
Query Processing and Optimization in Distributed Object-Oriented Databases - 1 -
Practice database programming with VB.net - 39
The real number 23.45425 is printed: 23.4543
The real number 678.0 is printed: 678.0000

+ When ios::scientific is on and the ios::showpoint flag is off, the real number is printed in exponential form (scientific form). The number of decimal places (after the decimal point) is mostly calculated with a precision of n, but the trailing zeros are omitted when printing.
For example, if the precision n = 4 then:
The real number -87.1500 is printed: -8.715e+01
The real number 23.45425 is printed: 2.3454e+01 The real number 678.0 is printed: 6.78e+02
+ When ios::scientific is on and the ios::showpoint flag is on, the real number is printed in exponential form. The number of decimal digits (after the decimal point) is printed exactly to the precision n.
For example, if the precision n = 4 then:
The real number -87.1500 is printed: -8.7150e+01 The real number 23.45425 is printed: 2.3454e+01 The real number 678.0 is printed: 6.7800e+01
Group 4: includes displays
ios::showpos, ios::showbase, ios::uppercase
ios::showpos flag
+ if the ios::showpos flag is off (default) then plus signs are not printed before positive numbers.
+ if the ios::showpos flag is set then a plus sign is printed before positive numbers.
ios::showbase flag
+ if the ios::showbase flag is set then octal integers are printed starting with the character 0 and hexadecimal integers are printed starting with the characters 0x. For example if a = 40 then:
Octal printing format is: 050 Hexadecimal printing format is 0x28
+ if the ios::showbase flag is off (default), do not print 0 before octal integers and do not print 0x before hexadecimal integers. For example, if a = 40 then:
octal printing is: 50
hexadecimal is 28
ios::uppercase flag
+ if the ios::uppercase flag is set, hexadecimal digits (like A, B, C, ...) are printed in uppercase.
+ if the ios::uppercase flag is off (default) then hexadecimal digits (like A, B, C, ...)
printed in lowercase.
5.5.3. Flag toggle methods
These methods are defined in the ios class.
Syntax: long cout.setf(long f);
Function: Enables the flags listed in f and returns a long value representing the flags.
is on. Usually the value of f is determined by a combination of flags. Example statement:
cout.setf(ios::showpoint | ios::scientific) ; will set the ios::showpoint and ios::scientific flags. Syntax: long cout.unsetf(long f);
Function: Turns off the flags listed in f and returns a long value representing the flags.
is on. Usually the value of f is determined by a combination of flags. Example statement:
cout.unsetf(ios::showpoint | ios::scientific); will disable the ios::showpoint and ios::scientific flags.
Syntax: long cout.flags(long f);
Function: Same as cout.setf(long). Example statement: cout.flags(ios::showpoint | ios::scientific) ;
will enable the ios::showpoint and ios::scientific flags.
Syntax: long cout.flags();
Function : Returns a long value representing the flags that are set.
5.6. Standard news streams
To import and export data to a file, we need to create new data streams (declare the
stream objects) and attach them to a specific file. C++ provides three stream classes to do this:
ofstream class: used to create output streams (file writing)
ifstream class: used to create input streams (read files)
ostream
fstream class: used for input, output or both input and output The derivation diagram is as follows:
iOS
fstreambase
istream
offstream
fstream
ifstream
Diagram 5.2
5.7. Write data to file
In C++, when working with a data file, the following steps need to be performed in sequence:
1) Open the file
2) Perform read and write operations on open files
3) Close the file
To perform operations related to data files, C++ provides a library <fstream.h> containing classes and functions for these operations. Therefore, in programs working with files, we need to declare the directive to use this library right at the beginning of the program: #include<fstream.h>
Declare file variable
In C++, when declaring a file variable, we will simultaneously open the corresponding file according to the general syntax using the fstream type as follows:
fstream <File variable name>(<File name>, <File open mode>); Where:
File variable name : has the same properties as a normal variable name, it will be used to perform operations with the file attached to it. File variable name must also follow the variable naming rules in C++.
File name : is the name of the data file that we need to operate on.
File open mode : is a bit-type constant predefined by C++. It indicates which mode we are opening the file in: read or write, or both read and write.
For example, declare:
fstream myFile( “ abc.txt ” , ios::in);
is to declare a file variable, named myFile, used to open a file named abc.txt and this file is opened in read mode (ios::in directive bit).
Note :
The file name is a string of characters. If you declare a file name with a directory path “ ” , each “ ” must be written as “ ” to avoid confusion with special characters in C such as “ n ” , “ d ” … For example, to open a file named abc.txt in the myDir directory for reading, you must declare it as follows:
fstream myFile( “ myDirabc.txt ” , ios::in);
File opening modes
File open modes are defined by indicator bits:
ios::in : Open a file for reading.
ios::out: Open an existing file for writing.
ios::app: Open an existing file to append data to the end of the file. ios::ate: Open the file and place the file pointer at the end of the file. ios::trunc : if the file already exists, the data in it will be lost. ios::nocreate : Open a file, the file must exist.
ios::noreplace : Open a file only if the file does not exist. ios::binary : Open a file in binary mode. ios::text : Open a file in text mode.
When we want to open a file in different modes at the same time, we combine the corresponding indicator bits using the bitwise operator “ | ” . For example, to open a file abc.txt for reading (ios::in) and writing (ios::out) in text mode (ios::text), we declare as follows:
fstream myFile( “ abc.txt ” , ios::in|ios::out|ios::text);
To open a file in text mode, we use the following syntax: fstream <File variable name>(<File name>, ios::text);
At that time, reading and writing operations on file variables are performed in units of words,
are separated by a space bar or a new line (enter). For example, to open the file baitho.txt in text mode, we declare as follows:
fstream myBaiTho( “ baitho.txt ” , ios::text);
To open a file in binary mode, we use the following syntax: fstream <File variable name>(<File name>, ios::binary);
At that time, the read and write operations on file variables are performed in byte units according to the size of the records (structures) recorded in the file. For example, to open the file baitho.txt in binary mode, we declare as follows:
fstream myBaiTho( “ baitho.txt ” , ios::binary);
Write text file with “ << ”
The steps to write data to a file are as follows:
B1. Open the file in write mode using the ofstream object (open the file for writing only):
ofstream <File variable name>(<File name>, ios::out); B2. Write data to file using “ << ” operator :
<File variable name> << <Data>; B3. Close the file using the close() command:
<File variable name>.close();
C++ allows us to define an overload of the file write operator (<<) on data types
object. Then, we must define an operator function as a multi-class friend function. For example, we define an overloaded file write operator on the ts class object data type mentioned in Example 5.3 as follows:
Example 5.5:
class ts
{
char sbd[5]; char name[30];
unsigned int score; public:
friend ofstream &operator<<(ofstream &fs, const ts &t)
{
}
....
};
fs<<t.sbd<<endl; fs<<t.name<<endl; fs<<t.diem<<endl; return fs;
Write data to a binary file
To write data to a binary file, follow these steps:
B1. Open the file in binary writing mode using the fstream object: fstream <File variable name>(<File name>, ios::out|ios::binary);
B2. Write data to file using write() command:
<File variable name>.write(char* <Data>, int <Data size>); B3. Close the file using the close() command:
<File variable name>.close();
In it, the write command takes two input parameters as follows:
The first parameter is a char pointer pointing to the data area to be written to the file. Because the pointer must be of type char, when we want to write data of a different type to the file, we use the type conversion function:
reinterpret_cast<char *>(<Data>);
The second parameter is the size of the data to be written to the file. This size is measured in bytes, so we usually use the operator:
sizeof(<Data Type>);
Note :
When we want to read or write structured data (struct) to a file, we must use binary file read/write mode and cannot use text read/write mode. When reading/writing structured data, for the sizeof() operator to work correctly, the structured fields cannot be pointer types. Because the sizeof() operator for pointers only gives the size of the pointer but does not give the actual size of the data area.
to which the pointer points.
The following program illustrates writing data to a binary file, the data is of structured type. The file name and the number of records are entered by the user from the keyboard. The program will write to the file the structured records entered by the user from the keyboard.
Example 5.6:
#include<stdlib.h>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<type.h>
const int length = 25; // Maximum length of file name typedef struct
{
int day; // Day
int month; // Month int year; // Year
} Date; typedef struct
{
char name[20]; // Employee name
Date birthDay; // Employee's date of birth char role[20]; // Employee's position float salary; // Employee's salary
} Employee; void main()
{
clrscr();
char fileName[length]; // File name cout << “ File name: ” ;
cin >> setw(length) >> fileName; // Enter file name int recordNumber; // Number of records
cout << “ Number of records: ” ;
cin >> recordNumber; // Enter the number of records
/* Open file */
// Declare and open the file
fstream fileOut(fileName, ios::out|ios::binary); if(!fileOut) // Unable to open file
{
cout << “ Cannot create file ” << fileName << endl; exit(1);
}
/* Write data to file */ Employee myEmployee;
for(int i=0; i<recordNumber; i++)
{
cout << “ Recorder ” << i+1 << endl; cout << “ Name: ” ;
cin >> myEmployee.name; // Enter employee name cout << “ Day of birth: ” ;
cin >> myEmployee.birthDay.day;// Enter date of birth cout << “ Month of birth: ” ;
cin >> myEmployee.birthDay.month;// Enter birth month cout << “ Year of birth: ” ;
cin >> myEmployee.birthDay.year;// Enter year of birth cout << “ Role: ” ;
cin >> myEmployee.role; // Enter position cout << “ Salary: ” ;
cin >> myEmployee.salary; // Enter salary
// Write data to file fileOut.write(reinterpret_cast<char *>(&myEmployee),





