SlideShare uma empresa Scribd logo
1 de 16
Chapter: 12


   Stream and Files
                Lecture: 49
              Date: 13.11.2012
Objectives
   Overview of stream classes

   Showing how to perform file-related activities using streams:
       How to read and write data to files in a variety of ways
       How to handle files or I/O errors
       How files and OOP are related
Streams
   Stream (flow of data)
       A transfer of information in the form of a sequence of bytes

    In C++, a stream is represented by an object of a
    particular class; e.g., cin and cout are objects of
    iostream class.

   I/O Operations:
       Input: A stream that flows from an input device ( i.e.: keyboard,
        disk drive, network connection) to main memory
       Output: A stream that flows from main memory to an output
        device ( i.e.: screen, printer, disk drive, network connection)
keyboard
                  standard
                input stream
                                CPU
                     standard
                      output    MEM
monitor               stream
terminal
console

                                HDD
What information travels
        across?
           Streams
keyboard
                 standard
               input stream
                                CPU

                  standard
                   output       MEM
monitor            stream
terminal                 file
console                 input
                       stream
                       LOAD     HDD
 What information      READ
 travels across?                          file
                                files   output
     Streams                            stream
                                        SAVE
                                        WRITE
Stream Class Hierarchy
What is a File?

   File: A file is a collection on information, usually
    stored on a computer’s disk. Information can be
    saved to files and then later reused.

   File Names: All files are assigned a name that is
    used for identification purposes by the operating
    system and the user.
File Names and Extensions
File Name and            File Contents
Extension
M Y P R O G .B A S       BASIC program
M E N U .B A T           DOS Batch File
IN S T A L L .D O C      Documentation File
C R U N C H .E X E       Executable File
B O B .H T M L           HTML (Hypertext Markup Language) File
3 D M O D E L .J A V A   Java program or applet
IN V E N T .O B J        Object File
P R O G 1 .P R J         Borland C++ Project File
A N S I.S Y S            System Device Driver
R E A D M E .T X T       Text File
Writing to a File



X

Y

Z
Reading From a File



X

Y

Z
Disk File I/O with Streams
   Most programs need to save data to disk files and read it
    back in.

   Working with disk files requires an other set of classes:
     ifstream for (file) input
     fstream for both (file) input and output
     ofstream for (file) output



   Objects of these classes can be associated with disk
    files, and their member functions can be used to
    read and write to files.
The Process of Using a File
   Using a file in a program is a simple three-step process
    1)   The file must be opened. If the file does not yet exits,
         opening it means creating it.
    2)   Information is then saved to the file, read from the file, or
         both.
    3)   When the program is finished using the file, the file must
         be closed.
#include <iostream>
#include <fstream>
#include <conio.h>                Writing to File
using namespace std;

int main()
{ char ch = ‘x’;
   int j = 77;
   double d = 6.02;
   string str1 = “Book”; //strings without embedded spaces
   string str2 = “Pen”;
ofstream outfile(“fdata.txt”); //create ofstream object
outfile << ch //insert (write) data
       << j
       << ‘ ‘ //needs space between numbers
       << d
       << str1
       << ‘ ‘ //needs spaces between strings
       << str2;
cout << “File writtenn”;
getch(); return 0; }
#include <iostream>
#include <fstream>
#include <conio.h>               Reading from File
using namespace std;

int main()
{ char ch;
    int j;
    double d;
    string str1;
    string str2;
ifstream infile("fdata.txt");//create ifstream object
                             //extract (read) data from it
infile >> ch >> j >> d >> str1 >> str2;

cout << ch << endl //display the data
     << j << endl
     << d << endl
     << str1 << endl
     << str2 << endl;
 getch(); return 0; }
Writing to File (easy way)
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;

int main () {
 ofstream myfile ("example.txt");
 if (myfile.is_open())
 {
   myfile << "This is a line.n";
   myfile << "This is another line.n";
   myfile.close();
 }
 else cout << "Unable to open file";

getch();
return 0; }
Reading from File (easy way)
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
     {
       getline (myfile, line); //extract characters into object myfile until line
       cout << line << endl;
     }
    myfile.close();
  }
 else cout << "Unable to open file";
getch(); return 0; }

Mais conteúdo relacionado

Mais procurados

Unix command line concepts
Unix command line conceptsUnix command line concepts
Unix command line conceptsArtem Nagornyi
 
Unix OS & Commands
Unix OS & CommandsUnix OS & Commands
Unix OS & CommandsMohit Belwal
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handlingpinkpreet_kaur
 
Linux command for beginners
Linux command for beginnersLinux command for beginners
Linux command for beginnersSuKyeong Jang
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Scriptsbmguys
 
Linux Introduction (Commands)
Linux Introduction (Commands)Linux Introduction (Commands)
Linux Introduction (Commands)anandvaidya
 
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Wave Digitech
 
Terminal Commands (Linux - ubuntu) (part-1)
Terminal Commands  (Linux - ubuntu) (part-1)Terminal Commands  (Linux - ubuntu) (part-1)
Terminal Commands (Linux - ubuntu) (part-1)raj upadhyay
 
50 most frequently used unix linux commands (with examples)
50 most frequently used unix   linux commands (with examples)50 most frequently used unix   linux commands (with examples)
50 most frequently used unix linux commands (with examples)Rodrigo Maia
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commandsSagar Kumar
 
Course 102: Lecture 2: Unwrapping Linux
Course 102: Lecture 2: Unwrapping Linux Course 102: Lecture 2: Unwrapping Linux
Course 102: Lecture 2: Unwrapping Linux Ahmed El-Arabawy
 

Mais procurados (20)

File handling
File handlingFile handling
File handling
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 
Unix command line concepts
Unix command line conceptsUnix command line concepts
Unix command line concepts
 
Basic 50 linus command
Basic 50 linus commandBasic 50 linus command
Basic 50 linus command
 
Basic linux commands
Basic linux commandsBasic linux commands
Basic linux commands
 
Unix OS & Commands
Unix OS & CommandsUnix OS & Commands
Unix OS & Commands
 
Unit 7
Unit 7Unit 7
Unit 7
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Basic Linux day 2
Basic Linux day 2Basic Linux day 2
Basic Linux day 2
 
Linux command for beginners
Linux command for beginnersLinux command for beginners
Linux command for beginners
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Script
 
Linux Introduction (Commands)
Linux Introduction (Commands)Linux Introduction (Commands)
Linux Introduction (Commands)
 
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013
 
Terminal Commands (Linux - ubuntu) (part-1)
Terminal Commands  (Linux - ubuntu) (part-1)Terminal Commands  (Linux - ubuntu) (part-1)
Terminal Commands (Linux - ubuntu) (part-1)
 
Linux commands
Linux commands Linux commands
Linux commands
 
50 most frequently used unix linux commands (with examples)
50 most frequently used unix   linux commands (with examples)50 most frequently used unix   linux commands (with examples)
50 most frequently used unix linux commands (with examples)
 
File management
File managementFile management
File management
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 
Unix
UnixUnix
Unix
 
Course 102: Lecture 2: Unwrapping Linux
Course 102: Lecture 2: Unwrapping Linux Course 102: Lecture 2: Unwrapping Linux
Course 102: Lecture 2: Unwrapping Linux
 

Semelhante a Lec 49 - stream-files

Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 
File handling in_c
File handling in_cFile handling in_c
File handling in_csanya6900
 
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 exampleSunil Patel
 
File management in C++
File management in C++File management in C++
File management in C++apoorvaverma33
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Filesprimeteacher32
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Rex Joe
 
Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式艾鍗科技
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))Papu Kumar
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceanuvayalil5525
 
Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-filesPrincess Sam
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++Shyam Gupta
 
Unix_Introduction_BCA.pptx the very basi
Unix_Introduction_BCA.pptx the very basiUnix_Introduction_BCA.pptx the very basi
Unix_Introduction_BCA.pptx the very basiPriyadarshini648418
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5YOGESH SINGH
 

Semelhante a Lec 49 - stream-files (20)

Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
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
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
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++
 
File management
File managementFile management
File management
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
Files in c++
Files in c++Files in c++
Files in c++
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
 
Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-files
 
Data file handling
Data file handlingData file handling
Data file handling
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Unix_Introduction_BCA.pptx the very basi
Unix_Introduction_BCA.pptx the very basiUnix_Introduction_BCA.pptx the very basi
Unix_Introduction_BCA.pptx the very basi
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 

Mais de Princess Sam

Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsPrincess Sam
 
Lec 40.41 - pointers
Lec 40.41 -  pointersLec 40.41 -  pointers
Lec 40.41 - pointersPrincess Sam
 
Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointersPrincess Sam
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functionsPrincess Sam
 
Lec 33 - inheritance
Lec 33 -  inheritanceLec 33 -  inheritance
Lec 33 - inheritancePrincess Sam
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritancePrincess Sam
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 

Mais de Princess Sam (12)

Lec 50
Lec 50Lec 50
Lec 50
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
Lec 40.41 - pointers
Lec 40.41 -  pointersLec 40.41 -  pointers
Lec 40.41 - pointers
 
Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointers
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functions
 
Lec 37 - pointers
Lec 37 -  pointersLec 37 -  pointers
Lec 37 - pointers
 
Lec 33 - inheritance
Lec 33 -  inheritanceLec 33 -  inheritance
Lec 33 - inheritance
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritance
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Lec 36 - pointers
Lec 36 -  pointersLec 36 -  pointers
Lec 36 - pointers
 

Lec 49 - stream-files

  • 1. Chapter: 12 Stream and Files Lecture: 49 Date: 13.11.2012
  • 2. Objectives  Overview of stream classes  Showing how to perform file-related activities using streams:  How to read and write data to files in a variety of ways  How to handle files or I/O errors  How files and OOP are related
  • 3. Streams  Stream (flow of data)  A transfer of information in the form of a sequence of bytes  In C++, a stream is represented by an object of a particular class; e.g., cin and cout are objects of iostream class.  I/O Operations:  Input: A stream that flows from an input device ( i.e.: keyboard, disk drive, network connection) to main memory  Output: A stream that flows from main memory to an output device ( i.e.: screen, printer, disk drive, network connection)
  • 4. keyboard standard input stream CPU standard output MEM monitor stream terminal console HDD What information travels across? Streams
  • 5. keyboard standard input stream CPU standard output MEM monitor stream terminal file console input stream LOAD HDD What information READ travels across? file files output Streams stream SAVE WRITE
  • 7. What is a File?  File: A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.  File Names: All files are assigned a name that is used for identification purposes by the operating system and the user.
  • 8. File Names and Extensions File Name and File Contents Extension M Y P R O G .B A S BASIC program M E N U .B A T DOS Batch File IN S T A L L .D O C Documentation File C R U N C H .E X E Executable File B O B .H T M L HTML (Hypertext Markup Language) File 3 D M O D E L .J A V A Java program or applet IN V E N T .O B J Object File P R O G 1 .P R J Borland C++ Project File A N S I.S Y S System Device Driver R E A D M E .T X T Text File
  • 9. Writing to a File X Y Z
  • 10. Reading From a File X Y Z
  • 11. Disk File I/O with Streams  Most programs need to save data to disk files and read it back in.  Working with disk files requires an other set of classes:  ifstream for (file) input  fstream for both (file) input and output  ofstream for (file) output  Objects of these classes can be associated with disk files, and their member functions can be used to read and write to files.
  • 12. The Process of Using a File  Using a file in a program is a simple three-step process 1) The file must be opened. If the file does not yet exits, opening it means creating it. 2) Information is then saved to the file, read from the file, or both. 3) When the program is finished using the file, the file must be closed.
  • 13. #include <iostream> #include <fstream> #include <conio.h> Writing to File using namespace std; int main() { char ch = ‘x’; int j = 77; double d = 6.02; string str1 = “Book”; //strings without embedded spaces string str2 = “Pen”; ofstream outfile(“fdata.txt”); //create ofstream object outfile << ch //insert (write) data << j << ‘ ‘ //needs space between numbers << d << str1 << ‘ ‘ //needs spaces between strings << str2; cout << “File writtenn”; getch(); return 0; }
  • 14. #include <iostream> #include <fstream> #include <conio.h> Reading from File using namespace std; int main() { char ch; int j; double d; string str1; string str2; ifstream infile("fdata.txt");//create ifstream object //extract (read) data from it infile >> ch >> j >> d >> str1 >> str2; cout << ch << endl //display the data << j << endl << d << endl << str1 << endl << str2 << endl; getch(); return 0; }
  • 15. Writing to File (easy way) #include <iostream> #include <fstream> #include <conio.h> using namespace std; int main () { ofstream myfile ("example.txt"); if (myfile.is_open()) { myfile << "This is a line.n"; myfile << "This is another line.n"; myfile.close(); } else cout << "Unable to open file"; getch(); return 0; }
  • 16. Reading from File (easy way) #include <iostream> #include <fstream> #include <conio.h> using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile, line); //extract characters into object myfile until line cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; getch(); return 0; }

Notas do Editor

  1. Student Book
  2. Used tootsie roll pieces as data bytes and a large tootsie roll as a line of data on a cardboard card and drawing on the chalkboard.
  3. Used tootsie roll pieces as data bytes and a large tootsie roll as a line of data on a cardboard card and drawing on the chalkboard.