Chapter 8 - Managing Text File
Chapter 8 - Managing Text File
CHAPTER 8:
Managing Text File
Contents :
4.Input Failure
5.Clear Function
i. Setprecicion manipulator
@reallygreatsite
• Cin Cout and Get Function
#include<iostream>
int main() {
CIN AND COUT
int my_int; STREAM:
std::cin >> my_int;
std::cout << my_int;
return 0;
}
@reallygreatsite
• Cin Cout and Get Function
Get Function
The cin.get() function in C++ can be used in two different ways. The first one is with specifying any parameters
for the function. And the second one is by specifying the parameters of the function.
cin.get()
• When we write the function as cin.get(), we are not specifying any condition or parameter for the
function. If done so, the parameters should be seen inside the parenthesis.
cin.get(string_name, size)
• When we write the function as cin.get(string name, size), which means we specify some condition or
parameter inside the parenthesis for the function.
@reallygreatsite
Get Function Example:
##include <iostream>
using namespace std;
return 0; OUTPUT
} INPUT
Get Function Example:
• Cin Cout and Get Function
#include<iostream>
int main() {
int my_int;
std::cin >> my_int;
std::cout << my_int;
return 0; }
int main()
Get Function {
The cin.get() function in C++ can be used in two different ways. The first one char name_1[25];
is with specifying any parameters for the function. And the second one is by cout<<"enter your full name: "<<endl;
specifying the parameters of the function. cin.get(name_1, 5);
cout << name_1;
cin.get() return 0;
• When we write the function as cin.get(), we are not specifying }
any condition or parameter for the function. If done so, the
parameters should be seen inside the parenthesis
cin.get(string_name, size)
• When we write the function as cin.get(string name, size),
which means we specify some condition or parameter inside
the parenthesis for the function.
2. Cin and Ignore function
Cin
• refers to "character" and "in" means "input".
• an object of the input stream and is used to take input from input streams.
Ignore Function
• ignore or clear one or more characters
• used to skip(discard/throw away) characters in the input stream.
Conti…
Cin example :
In this program, the user is prompted to enter a number and their
#include <iostream>
name. The cin object is used with the insertion operator (>>) to
#include <string> read the input values.
using namespace std;
int main() After reading the number and name, the program prints them out
for verification.
{
int number; Cin reads strings until first whitespace character, storing first
string name; word in name variable. Getline function reads entire input line.
cout<<"Enter number: ";
cin>>number; OUTPUT:
Enter number : 18
Number : 18
cout<<"Number: " << number <<endl;
@reallygreatsite
return 0;
}
Example ..Cin and Ignore
Ignore function example :
#include<iostream> function In this example, we didn’t get the desired output from the
#include<vector>
using namespace std; program. We have taken two cin input streams, one for the num
and the other from the string name, but only the num value is
int main() { taken.
int num;
It ignored the getline() without taking any input value. In this
char name[20];
case, the value goes to the num variable buffer, as a result, we
cout << "Enter your Roll Number and Name:\n"; didn’t get the string value.
cin >> num;
The cin object is a standard input stream in C++ that is used for reading input from the user. It is
part of the iostream library and is commonly used for console input.
The ignore() function is a member function of the istream class, which is the base class for input
streams in C++. The ignore() function is used to discard characters from the input stream.
The ignore() function has several overloaded versions, but the most commonly used one takes two
arguments: the number of characters to ignore and an optional delimiter. The function discards
characters until it has ignored the specified number of characters or until it encounters the
delimiter.
3. Putback and peek function
The putback function lets you put the last character extracted from the input stream by the get
function back into the input stream. The stream function peek looks into the stream and tells you
what the next character is without removing it from the input stream.
return 0;
4.Input Failure
• Inputs is extremely important because , an unhandled wrong input might have the complete ability to
crash a system. I#include<iostream>#include<limits>using
namespace std;int main(){int a;
if ( cin.fail ()) {
cout <<"input failure occured"<< endl;
Conti..
When input data doesn't match associated variables
When a program is expecting a number and the user enter a letter or character that
doesn't translate to a number
• The clear() function is a member function of the std::basic_ios class, used to reset error state
flags and clear associated error states. It is used to clear error state flags on a stream object,
allowing further operations. Parameters can be passed to modify the function's behavior,
such as a specific error state value or an error category. It is crucial to note that clear() only
clears error flags and does not affect the stream's contents or internal get and put pointers.
Example CLEAR
FUNCTION
6. Input and Formatting Output
In C++, input and formatting output can be done using standard C++ input/output streams, namely std::cin and
std::cout
i.setprecision manipulator
What is setprecision manipulator ?
• SetPrecision is a function in the C++ • setprecision() method in C++ is an inbuilt
standard library's <iomanip> header that is method that is used to manipulate floating-
used to set the precision (i.e., the number of point values. It is used to set the precision
decimal places) for floating-point values of the floating-point numbers after the
when using the stream insertion operator decimal.
(<<) with a std::cout stream.
Example
• The set precision function of the C++ manipulator is used to adjust the number of digits
displayed in a floating-point value output stream.
C++ Manipulator setprecision
• The set precision function of the C++ manipulator is used to control the number of digits shown in
an output stream of a floating-point value.
• declared in header file <iomanip>.
• Syntax
• /*unspecified*/ set precision (int n);
• Parameter
• n: the new value for the decimal precision.
• RETURN value
An unspecified type of object is returned by this function. Only stream manipulators should use the
database function.
• Data Races
Data races can occur when multiple users simultaneously access the same stream object, which is
modified when the data is inserted or extracted.
• Exceptions
If any exception is raised, the object is in a valid state.
ii. Fixed Manipulator
Example:
// C++ code to demonstrate
fixed() method of stream manipulator // the working of fixed() function
#include <iostream>
to set the floatfield format flag for the
using namespace std;
specified str stream.
int main()
{
When set floatfield to fixed, floating point values are Output:
// Initializing the float values without fixed flag: 1.23
written using fixed notation; the value is represented double x = 1.23; with fixed flag: 1.23000
with exactly as many digits in the decimal part as
cout.precision(5);
specified by the precision filed (precision) and with
no exponent part. cout << "without fixed flag: "
<< x << endl;
return 0;
}
iii.Showpoint Manipulator [c++ ]
• The `showpoint` manipulator is used to display the decimal point and trailing zeros for floating-point numbers when outputting
them. It is an I/O manipulator that affects the behavior of the `std::cout` stream.
• When you output a floating-point number using `std::cout`, only the necessary number of digits are displayed. The `showpoint`
manipulator allows you to control the display of decimal points and trailing zeros, even if they are not necessary for the
number's precision.
• The `showpoint` manipulator can be used with any stream that supports the `ios_base::showpoint` flag. This includes the `cout`
and `cerr` streams.
CODING OUTPUT
iv.Setfill Manipulator
The fill character for output streams is set using the
setfill manipulator. It is frequently used to format output
and is a component of the iomanip> header.
OUTPUT :-
EXAMPLE :-
#include <iostream>
#include <iomanip>
int main() {
int number = 42;
std::cout << std::setw(10) << std::setfill('*') << number << std::endl;
return 0;
}
v. Left & Right Manipulators.
Example
Left Manipulators.
• The left() method is used to set the adjustfield format
flag for the specified str stream.
• Sets the adjustfield to left.
• The output will be padded to the field width by
inserting fill characters at the end.
Right Manipulators.
• The right() method is used to set the adjustfield format
flag for the specified str stream.
• This flag sets the adjustfield to right.
• The output will be padded to the field width by
inserting fill characters at the start.
vi.Input / Output and String Type
• A string data type is traditionally a sequence of characters, either as a literal constant or as some kind of variable.
The latter may allow its elements to be mutated and the length changed, or it may be fixed (after creation).
• A primary purpose of strings is to store human-readable text, like words and sentences. Strings are used to
communicate information from a computer program to the user of the program. A program may also accept string
input from its user. Further, strings may store data expressed as characters yet not intended for human reading.
Example ;
For example, consider the following program:
vii.Debugging: Understanding Logic Errors and
cout statement
DEFINITION:
Debugging is the process of finding logic errors in
LOGIC
your program. A logic error is an error that the
compiler will not find (an error that the compiler
ERROR
will find is a syntax error).
HOW TO DEBUG?
There are several ways to debug a C++ program. Here's a
list, and we'll talk about each of them:
• Check the program logic by hand
• Insert cout statements in the program
• Use a symbolic debugge
This program will compile, and when run will print out the string
"5 is greater than 7". Obviously this is incorrect, and is an
example of a logic error. The compiler cannot help you find logic
errors.
vii.File input output: fstream
<fstream> library provides functions for files and should be simply added with #include <fstream> directives
at the start of our program.
C++ provides the following classes to perform output and input of characters to/from files:
• ofstream: Stream class to write on files
• ifstream: Stream class to read from files
• fstream: Stream class that has the ability to handle both ifstream and ofstream. It can be used to read and
write from/to files.
These classes are derived directly or indirectly from the classes istream and ostream. We have already used
objects whose types were these classes: cin is an object of class istream, and cout is an object of class ostream.
Therefore, we have already been using classes that are related to our file streams. In fact, we can use our file
streams the same way we are already used to using cin and cout, with the only difference that we have to
associate these streams with physical files.
Example of File input output:
Let's see an example:
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
} • This code creates a file called example.txt and inserts a sentence into it in the same way we are
used to do with cout, but using the file stream myfile instead.