SlideShare a Scribd company logo
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

Python-files
Python-filesPython-files
Python-files
Krishna Nanda
 
file handling, dynamic memory allocation
file handling, dynamic memory allocationfile handling, dynamic memory allocation
file handling, dynamic memory allocation
indra Kishor
 
[Java] #7 - Input & Output Stream
[Java] #7 - Input & Output Stream[Java] #7 - Input & Output Stream
[Java] #7 - Input & Output Stream
Ghadeer AlHasan
 
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
Martijn Verburg
 
17 files and streams
17 files and streams17 files and streams
17 files and streams
Docent Education
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
Hitesh Kumar
 
C++ files and streams
C++ files and streamsC++ files and streams
C++ files and streams
krishna partiwala
 
Linux intro 2 basic terminal
Linux intro 2   basic terminalLinux intro 2   basic terminal
Linux intro 2 basic terminal
Giovanni Marco Dall'Olio
 
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
Mahendra Yadav
 
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
Sunil Patel
 
Linux intro 3 grep + Unix piping
Linux intro 3 grep + Unix pipingLinux intro 3 grep + Unix piping
Linux intro 3 grep + Unix piping
Giovanni Marco Dall'Olio
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
Shyam Gupta
 
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
ramya marichamy
 
Input File dalam C++
Input File dalam C++Input File dalam C++
Input File dalam C++Teguh Nugraha
 
File handling
File handlingFile handling
File handling
Ans Ali
 
Linux intro 4 awk + makefile
Linux intro 4  awk + makefileLinux intro 4  awk + makefile
Linux intro 4 awk + makefile
Giovanni Marco Dall'Olio
 

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.ppt
HEMAHEMS5
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
Praveen M Jigajinni
 
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
ANUSUYA S
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
data file handling
data file handlingdata file handling
data file handling
krishna partiwala
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
Dr .Ahmed Tawwab
 
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.pdf
aquacare2008
 
chapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfchapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdf
study material
 
Files and streams
Files and streamsFiles and streams
Files and streams
Pranali Chaudhari
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
YOGESH SINGH
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
sanya6900
 
C++ppt.pptx
C++ppt.pptxC++ppt.pptx
C++ppt.pptx
MrGyanprakash
 

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
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
 
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
 

More from Ananda Kumar HN

DAA 18CS42 VTU CSE
DAA 18CS42 VTU CSEDAA 18CS42 VTU CSE
DAA 18CS42 VTU CSE
Ananda Kumar HN
 
CGV 18CS62 VTU CSE
CGV 18CS62 VTU CSECGV 18CS62 VTU CSE
CGV 18CS62 VTU CSE
Ananda Kumar HN
 
Python for beginners
Python for beginnersPython for beginners
Python for beginners
Ananda Kumar HN
 
VTU Network lab programs
VTU Network lab   programsVTU Network lab   programs
VTU Network lab programs
Ananda Kumar HN
 
VTU CN-1important questions
VTU CN-1important questionsVTU CN-1important questions
VTU CN-1important questions
Ananda 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 introduction
Ananda 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 Inheritance
Ananda Kumar HN
 
C++ prgms 3rd unit
C++ prgms 3rd unitC++ prgms 3rd unit
C++ prgms 3rd unit
Ananda Kumar HN
 
Microsoft office in kannada for begineers
Microsoft office in kannada for begineersMicrosoft office in kannada for begineers
Microsoft office in kannada for begineers
Ananda Kumar HN
 
Microsoft office in KANNADA
Microsoft office in KANNADAMicrosoft office in KANNADA
Microsoft office in KANNADA
Ananda 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

Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 

Recently uploaded (20)

Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 

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