SlideShare a Scribd company logo
1 of 3
Download to read offline
I/O System Basics, File I/O UNIT 7
1 | P a g e A n a n d a K u m a r H N
I/O System Basics, File I/0: C++ stream classes, Formatted I/O, I/O manipulators,
fstream and the File classes, File operations
/* Normal C++ output*/
#include<iostream>
using namespace std;
int main()
{
cout<<"ATME"<<endl<<endl;
}
OUTPUT:
/*Formatted output*/
#include<iostream>
using namespace std;
int main()
{
cout.setf(ios::right);
cout.width(20);
cout<<"ATME"<<endl<<endl;
}
OUTPUT:
/* width( ) fill( ) setf( ) */
Example 1:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout.precision(7);
cout.fill('*'); //fill function
cout.width(19); // width function
cout.setf(ios::right);// setting flag
cout<<52.1234567<<endl;
cout<<setfill('?')<<setw(30);
cout<<"ATME"<<endl<<endl;
}
OUTPUT:
Example 2:
#include<iostream>
using namespace std;
int main()
{
cout.setf(ios::right);
cout.fill('$');
cout.width(20);
cout<<"ATME"<<endl<<endl;
}
OUTPUT:
/* showpos flag */
#include<iostream>
using namespace std;
int main()
{
int x=1000;
cout.setf(ios::showpos);
cout.fill('$');
cout.width(10);
cout<<"x="<<x<<endl<<endl;
}
OUTPUT:
/* showpoint flag */
#include<iostream>
using namespace std;
int main()
{
float x=1000;
cout.setf(ios::showpos);
cout.setf(ios::showpoint);
cout.fill('$');
cout.width(10);
cout<<"x="<<x<<endl<<endl;
}
OUTPUT:
I/O System Basics, File I/O UNIT 7
2 | P a g e A n a n d a K u m a r H N
/* Scientific flag */
#include<iostream>
using namespace std;
int main()
{
float x=1000.123;
cout.setf(ios::scientific);
cout.width(10);
cout<<"x="<<x<<endl<<endl;
}
OUTPUT:
#include<iostream>
using namespace std;
int main()
{
cout.precision(7);
cout.fill('*'); //fill function
cout.width(19); // width function
cout.setf(ios::left);// setting flag
cout.setf(ios::scientific);
cout<<52.1234567<<endl;
cout<<endl<<"unsetting flags"<<endl;
cout.unsetf(ios::left);
cout.unsetf(ios::scientific);
cout<<52.1234567<<endl;
}
OUTPUT:
/*C++ Manipulators */
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float x=188.9298;
cout<<hex<<100<<endl;
cout<<setfill('#')<<setw(20)<<x;
}
OUTPUT:
fstream and the File classes:
• ofstream: Stream class to write on files
• ifstream: Stream class to read from files
• fstream: Stream class to both 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.
Therfore, we have already been using classes that are
related to our file streams. And in fact, we can use our
filestreams the same way we are already used to use
cin and cout, with the only difference that we have to
associate these streams with physical files.
Let's see an example:
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.
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.n";
myfile.close();
return 0;
}
OUTPUT:
This program will create a txt file with filename as
example and it writes line "Writing this to a file.” And
closes the file.
Open a file:
The first operation generally performed on an object of
one of these classes is to associate it to a real file. This
procedure is known as to open a file. An open file is
represented within a program by a stream object
(an instantiation of one of these classes, in the previous
example this was myfile) and any input or output
operation performed on this stream object will be
applied to the physical file associated to it.
In order to open a file with a stream object we use its
member function open():
open (filename, mode);
Where filename is a null-terminated character
sequence of type const char * (the same type that
I/O System Basics, File I/O UNIT 7
2 | P a g e A n a n d a K u m a r H N
/* Scientific flag */
#include<iostream>
using namespace std;
int main()
{
float x=1000.123;
cout.setf(ios::scientific);
cout.width(10);
cout<<"x="<<x<<endl<<endl;
}
OUTPUT:
#include<iostream>
using namespace std;
int main()
{
cout.precision(7);
cout.fill('*'); //fill function
cout.width(19); // width function
cout.setf(ios::left);// setting flag
cout.setf(ios::scientific);
cout<<52.1234567<<endl;
cout<<endl<<"unsetting flags"<<endl;
cout.unsetf(ios::left);
cout.unsetf(ios::scientific);
cout<<52.1234567<<endl;
}
OUTPUT:
/*C++ Manipulators */
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float x=188.9298;
cout<<hex<<100<<endl;
cout<<setfill('#')<<setw(20)<<x;
}
OUTPUT:
fstream and the File classes:
• ofstream: Stream class to write on files
• ifstream: Stream class to read from files
• fstream: Stream class to both 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.
Therfore, we have already been using classes that are
related to our file streams. And in fact, we can use our
filestreams the same way we are already used to use
cin and cout, with the only difference that we have to
associate these streams with physical files.
Let's see an example:
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.
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.n";
myfile.close();
return 0;
}
OUTPUT:
This program will create a txt file with filename as
example and it writes line "Writing this to a file.” And
closes the file.
Open a file:
The first operation generally performed on an object of
one of these classes is to associate it to a real file. This
procedure is known as to open a file. An open file is
represented within a program by a stream object
(an instantiation of one of these classes, in the previous
example this was myfile) and any input or output
operation performed on this stream object will be
applied to the physical file associated to it.
In order to open a file with a stream object we use its
member function open():
open (filename, mode);
Where filename is a null-terminated character
sequence of type const char * (the same type that

More Related Content

What's hot (20)

File handling
File handlingFile handling
File handling
 
Python-files
Python-filesPython-files
Python-files
 
file handling, dynamic memory allocation
file handling, dynamic memory allocationfile handling, dynamic memory allocation
file handling, dynamic memory allocation
 
Java Week4(B) Notepad
Java Week4(B)   NotepadJava Week4(B)   Notepad
Java Week4(B) Notepad
 
[Java] #7 - Input & Output Stream
[Java] #7 - Input & Output Stream[Java] #7 - Input & Output Stream
[Java] #7 - Input & Output Stream
 
Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2
 
17 files and streams
17 files and streams17 files and streams
17 files and streams
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
C++ files and streams
C++ files and streamsC++ files and streams
C++ files and streams
 
Linux intro 2 basic terminal
Linux intro 2   basic terminalLinux intro 2   basic terminal
Linux intro 2 basic terminal
 
File Handling and Command Line Arguments in C
File Handling and Command Line Arguments in CFile Handling and Command Line Arguments in C
File Handling and Command Line Arguments in C
 
File Pointers
File PointersFile Pointers
File Pointers
 
Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with example
 
Linux intro 3 grep + Unix piping
Linux intro 3 grep + Unix pipingLinux intro 3 grep + Unix piping
Linux intro 3 grep + Unix piping
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with files
 
Input File dalam C++
Input File dalam C++Input File dalam C++
Input File dalam C++
 
File handling
File handlingFile handling
File handling
 
Linux intro 4 awk + makefile
Linux intro 4  awk + makefileLinux intro 4  awk + makefile
Linux intro 4 awk + makefile
 
Oop lecture9 11
Oop lecture9 11Oop lecture9 11
Oop lecture9 11
 

Similar to C++ prgms io file unit 7

Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operationsTAlha MAlik
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handlingpinkpreet_kaur
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 
csc1201_lecture13.ppt
csc1201_lecture13.pptcsc1201_lecture13.ppt
csc1201_lecture13.pptHEMAHEMS5
 
Input output files in java
Input output files in javaInput output files in java
Input output files in javaKavitha713564
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
C++  - UNIT_-_V.pptx which contains details about File ConceptsC++  - UNIT_-_V.pptx which contains details about File Concepts
C++ - UNIT_-_V.pptx which contains details about File ConceptsANUSUYA S
 
File management in C++
File management in C++File management in C++
File management in C++apoorvaverma33
 
ExplanationThe files into which we are writing the date area called.pdf
ExplanationThe files into which we are writing the date area called.pdfExplanationThe files into which we are writing the date area called.pdf
ExplanationThe files into which we are writing the date area called.pdfaquacare2008
 
chapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfchapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfstudy material
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5YOGESH SINGH
 
File handling in_c
File handling in_cFile handling in_c
File handling in_csanya6900
 

Similar to C++ prgms io file unit 7 (20)

Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
csc1201_lecture13.ppt
csc1201_lecture13.pptcsc1201_lecture13.ppt
csc1201_lecture13.ppt
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
C++  - UNIT_-_V.pptx which contains details about File ConceptsC++  - UNIT_-_V.pptx which contains details about File Concepts
C++ - UNIT_-_V.pptx which contains details about File Concepts
 
data file handling
data file handlingdata file handling
data file handling
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
File management in C++
File management in C++File management in C++
File management in C++
 
Satz1
Satz1Satz1
Satz1
 
ExplanationThe files into which we are writing the date area called.pdf
ExplanationThe files into which we are writing the date area called.pdfExplanationThe files into which we are writing the date area called.pdf
ExplanationThe files into which we are writing the date area called.pdf
 
chapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfchapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdf
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
File handling in cpp
File handling in cppFile handling in cpp
File handling in cpp
 
C++ppt.pptx
C++ppt.pptxC++ppt.pptx
C++ppt.pptx
 
Filesin c++
Filesin c++Filesin c++
Filesin c++
 

More from Ananda Kumar HN

VTU Network lab programs
VTU Network lab   programsVTU Network lab   programs
VTU Network lab programsAnanda Kumar HN
 
VTU CN-1important questions
VTU CN-1important questionsVTU CN-1important questions
VTU CN-1important questionsAnanda Kumar HN
 
Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introductionAnanda Kumar HN
 
C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)Ananda Kumar HN
 
C++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceC++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceAnanda Kumar HN
 
Microsoft office in kannada for begineers
Microsoft office in kannada for begineersMicrosoft office in kannada for begineers
Microsoft office in kannada for begineersAnanda Kumar HN
 
Microsoft office in KANNADA
Microsoft office in KANNADAMicrosoft office in KANNADA
Microsoft office in KANNADAAnanda Kumar HN
 

More from Ananda Kumar HN (11)

DAA 18CS42 VTU CSE
DAA 18CS42 VTU CSEDAA 18CS42 VTU CSE
DAA 18CS42 VTU CSE
 
CGV 18CS62 VTU CSE
CGV 18CS62 VTU CSECGV 18CS62 VTU CSE
CGV 18CS62 VTU CSE
 
Python for beginners
Python for beginnersPython for beginners
Python for beginners
 
VTU Network lab programs
VTU Network lab   programsVTU Network lab   programs
VTU Network lab programs
 
VTU CN-1important questions
VTU CN-1important questionsVTU CN-1important questions
VTU CN-1important questions
 
Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introduction
 
C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)
 
C++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceC++ prgms 4th unit Inheritance
C++ prgms 4th unit Inheritance
 
C++ prgms 3rd unit
C++ prgms 3rd unitC++ prgms 3rd unit
C++ prgms 3rd unit
 
Microsoft office in kannada for begineers
Microsoft office in kannada for begineersMicrosoft office in kannada for begineers
Microsoft office in kannada for begineers
 
Microsoft office in KANNADA
Microsoft office in KANNADAMicrosoft office in KANNADA
Microsoft office in KANNADA
 

Recently uploaded

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 

C++ prgms io file unit 7

  • 1. I/O System Basics, File I/O UNIT 7 1 | P a g e A n a n d a K u m a r H N I/O System Basics, File I/0: C++ stream classes, Formatted I/O, I/O manipulators, fstream and the File classes, File operations /* Normal C++ output*/ #include<iostream> using namespace std; int main() { cout<<"ATME"<<endl<<endl; } OUTPUT: /*Formatted output*/ #include<iostream> using namespace std; int main() { cout.setf(ios::right); cout.width(20); cout<<"ATME"<<endl<<endl; } OUTPUT: /* width( ) fill( ) setf( ) */ Example 1: #include<iostream> #include<iomanip> using namespace std; int main() { cout.precision(7); cout.fill('*'); //fill function cout.width(19); // width function cout.setf(ios::right);// setting flag cout<<52.1234567<<endl; cout<<setfill('?')<<setw(30); cout<<"ATME"<<endl<<endl; } OUTPUT: Example 2: #include<iostream> using namespace std; int main() { cout.setf(ios::right); cout.fill('$'); cout.width(20); cout<<"ATME"<<endl<<endl; } OUTPUT: /* showpos flag */ #include<iostream> using namespace std; int main() { int x=1000; cout.setf(ios::showpos); cout.fill('$'); cout.width(10); cout<<"x="<<x<<endl<<endl; } OUTPUT: /* showpoint flag */ #include<iostream> using namespace std; int main() { float x=1000; cout.setf(ios::showpos); cout.setf(ios::showpoint); cout.fill('$'); cout.width(10); cout<<"x="<<x<<endl<<endl; } OUTPUT:
  • 2. I/O System Basics, File I/O UNIT 7 2 | P a g e A n a n d a K u m a r H N /* Scientific flag */ #include<iostream> using namespace std; int main() { float x=1000.123; cout.setf(ios::scientific); cout.width(10); cout<<"x="<<x<<endl<<endl; } OUTPUT: #include<iostream> using namespace std; int main() { cout.precision(7); cout.fill('*'); //fill function cout.width(19); // width function cout.setf(ios::left);// setting flag cout.setf(ios::scientific); cout<<52.1234567<<endl; cout<<endl<<"unsetting flags"<<endl; cout.unsetf(ios::left); cout.unsetf(ios::scientific); cout<<52.1234567<<endl; } OUTPUT: /*C++ Manipulators */ #include<iostream> #include<iomanip> using namespace std; int main() { float x=188.9298; cout<<hex<<100<<endl; cout<<setfill('#')<<setw(20)<<x; } OUTPUT: fstream and the File classes: • ofstream: Stream class to write on files • ifstream: Stream class to read from files • fstream: Stream class to both 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. Therfore, we have already been using classes that are related to our file streams. And in fact, we can use our filestreams the same way we are already used to use cin and cout, with the only difference that we have to associate these streams with physical files. Let's see an example: 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. // basic file operations #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.n"; myfile.close(); return 0; } OUTPUT: This program will create a txt file with filename as example and it writes line "Writing this to a file.” And closes the file. Open a file: The first operation generally performed on an object of one of these classes is to associate it to a real file. This procedure is known as to open a file. An open file is represented within a program by a stream object (an instantiation of one of these classes, in the previous example this was myfile) and any input or output operation performed on this stream object will be applied to the physical file associated to it. In order to open a file with a stream object we use its member function open(): open (filename, mode); Where filename is a null-terminated character sequence of type const char * (the same type that
  • 3. I/O System Basics, File I/O UNIT 7 2 | P a g e A n a n d a K u m a r H N /* Scientific flag */ #include<iostream> using namespace std; int main() { float x=1000.123; cout.setf(ios::scientific); cout.width(10); cout<<"x="<<x<<endl<<endl; } OUTPUT: #include<iostream> using namespace std; int main() { cout.precision(7); cout.fill('*'); //fill function cout.width(19); // width function cout.setf(ios::left);// setting flag cout.setf(ios::scientific); cout<<52.1234567<<endl; cout<<endl<<"unsetting flags"<<endl; cout.unsetf(ios::left); cout.unsetf(ios::scientific); cout<<52.1234567<<endl; } OUTPUT: /*C++ Manipulators */ #include<iostream> #include<iomanip> using namespace std; int main() { float x=188.9298; cout<<hex<<100<<endl; cout<<setfill('#')<<setw(20)<<x; } OUTPUT: fstream and the File classes: • ofstream: Stream class to write on files • ifstream: Stream class to read from files • fstream: Stream class to both 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. Therfore, we have already been using classes that are related to our file streams. And in fact, we can use our filestreams the same way we are already used to use cin and cout, with the only difference that we have to associate these streams with physical files. Let's see an example: 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. // basic file operations #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.n"; myfile.close(); return 0; } OUTPUT: This program will create a txt file with filename as example and it writes line "Writing this to a file.” And closes the file. Open a file: The first operation generally performed on an object of one of these classes is to associate it to a real file. This procedure is known as to open a file. An open file is represented within a program by a stream object (an instantiation of one of these classes, in the previous example this was myfile) and any input or output operation performed on this stream object will be applied to the physical file associated to it. In order to open a file with a stream object we use its member function open(): open (filename, mode); Where filename is a null-terminated character sequence of type const char * (the same type that