SlideShare uma empresa Scribd logo
1 de 3
Baixar para ler 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

Mais conteúdo relacionado

Mais procurados (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
 

Semelhante a 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
 

Semelhante a 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++
 

Mais de 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
 

Mais de 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
 

Último

Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 

Último (20)

Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

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