File I/O in C++. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk);is stored on a secondary storage device.

Slides:



Advertisements
Similar presentations
Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
Advertisements

File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
計算機概論實習 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker n-1 end-of-file marker 67 This is.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Tutorial4us.com. File A file is a collection of related data stored in a particular area on the disk. The data is stored in disk using the concept of.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
Chapter 3: Input/Output
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
C++ Lecture 8 Monday, 25 August I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
FILE HANDLING IN C++.
Programming Principles II Lecture Notes 7 Files Andreas Savva.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
File handling in C++ BCA Sem III K.I.R.A.S. Using Input/Output Files Files in C++ are interpreted as a sequence of bytes stored on some storage media.
1 firstlastnextprevioushome richard jones COMP103A Introduction to Computer Science 1 Richard Jones G2.04
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
I/O in C++ October 7, Junaed Sattar. Stream I/O a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output.
C++ FILE I/O.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
C++ Programming: chapter 6 – iostream 2014, Spring Pusan National University Ki-Joune Li 1.
COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
DCT1063 Programming 2 CHAPTER 3 FILE INPUT AND FILE OUTPUT Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Streams, and File I/O Review. STREAMS Review STREAMS Streams are sequences of bytes. C++ I/0 occurs in streams Input – bytes flow from device to memory.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 CSC241: Object Oriented Programming Lecture No 32.
File Handling in C++.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
C++ Programming: chapter 6 – iostream 2015, Spring Pusan National University Ki-Joune Li 1.
Learners Support Publications Working with Files.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
Lecture 14 Arguments, Classes and Files. Arguments.
Binary Files. Text Files vs. Binary Files Text files: A way to store data in a secondary storage device using Text representation (e.g., ASCII characters)
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
KBD Program with file I/O Operation File Disk Program with file I/O Operation Screen Cin Cout.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
CS212: Object Oriented Analysis and Design
CS212: Object Oriented Analysis and Design
CS-103 COMPUTER PROGRAMMING
Tutorial4us.com File Handling in C++ Tutorial4us.com.
What is a 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.
Basic Input and Output Operations
FILE HANDLING IN C++.
files Dr. Bhargavi Goswami Department of Computer Science
Tutorial4us.com. File A file is a collection of related data stored in a particular area on the disk. The data is stored in disk using the concept of.
Topics Input and Output Streams More Detailed Error Testing
File I/O.
File I/O in C++ I.
Data File Handling in C++
Programming File I/O.
CHAPTER 4 File Processing.
C++ Programming: chapter 6 – iostream
Data File Handling RITIKA SHARMA.
Reading from and Writing to Files
C++ Programming Lecture 8 File Processing
File I/O in C++ II.
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

File I/O in C++

Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk);is stored on a secondary storage device (e.g., disk); is permanent;is permanent; can be used to provide input data to a program or receive output data from a program, or both;can be used to provide input data to a program or receive output data from a program, or both; should reside in Project directory for easy access;should reside in Project directory for easy access; must be opened before it is used.must be opened before it is used.

General File I/O Steps Declare a file name variable Associate the file name variable with the disk file name Open the file Use the file Close the file

Using Input/Output Files stream - a sequence of characters stream - a sequence of characters interactive (iostream)interactive (iostream)  cin - input stream associated with keyboard.  cout - output stream associated with display. file (fstream)file (fstream)  ifstream - defines new input stream (normally associated with a file).  ofstream - defines new output stream (normally associated with a file).

Stream I/O Library Header Files Note: There is no “.h” on standard header files : Note: There is no “.h” on standard header files : iostream -- contains basic information required for all stream I/O operations iostream -- contains basic information required for all stream I/O operations iomanip -- contains information useful for performing formatted I/O with parameterized stream manipulators iomanip -- contains information useful for performing formatted I/O with parameterized stream manipulators fstream -- contains information for performing file I/O operations fstream -- contains information for performing file I/O operations strstream -- contains information for performing in-memory I/O operations (i.e., into or from strings in memory) strstream -- contains information for performing in-memory I/O operations (i.e., into or from strings in memory)

ios is the base class. ios is the base class. istream and ostream inherit from ios istream and ostream inherit from ios ifstream inherits from istream (and ios) ifstream inherits from istream (and ios) ofstream inherits from ostream (and ios) ofstream inherits from ostream (and ios) iostream inherits from istream and ostream (& ios) iostream inherits from istream and ostream (& ios) fstream inherits from ifstream, iostream, and ofstream fstream inherits from ifstream, iostream, and ofstream Classes for Stream I/O in C++

C++ streams #include #include int main (void) { //Local declarations //Local declarations ifstream fsIn; ifstream fsIn; ofstream fsOut; ofstream fsOut;... return 0; return 0;}

input_stream.open("numbers.dat") Stream handle Name Calling Object Dot Operator Member Function Name File Name Object and Member Functions

File I/O Example: Writing #include #include using namespace std; int main(void) { ofstream outFile(“fout.txt"); outFile << "Hello World!"; outFile.close(); return 0; }

File I/O Example: Writing #include #include using namespace std; int main(void) { ofstream outFile; outFile.open(“fout.txt”); outFile.open(“fout.txt”); outFile << “First line”; //behave just like cout outFile.close(); outFile<<“Another line”<<endl; //?? outFile<<“Another line”<<endl; //?? return 0; }

File I/O Example: Reading #include #include int main(void) { ifstream openFile(“data.txt"); //open a text file data.txt char ch; while(!OpenFile.eof()){OpenFile.get(ch); cout << ch; }OpenFile.close(); return 0; return 0;}

File I/O Example: Reading #include #include int main(void) { ifstream openFile(“data.txt"); //Declare and open a text file string line; while(!openFile.eof()){ getline(openFile,line);//fetch line from data.txt and put it in a string cout << line; }openFile.close(); return 0; return 0;}

File I/O Example: Reading #include #include int main(void) { ifstream openFile(“data.txt"); //open a text file data.txt string line; if(openFile.is_open()){ // if(openFile.is_open()){ //while(!openFile.eof()){ getline(openFile,line);//read a line from data.txt and put it in a string cout << line; } else{ else{ cout<<“File does not exist!”<<endl; cout<<“File does not exist!”<<endl; exit(1);} exit(1);} }openFile.close(); return 0; return 0;}

More Input File-Related Functions ifstream fsin; ifstream fsin; fsIn.open(const char[] fname) fsIn.open(const char[] fname) connects stream fsIn to the external file fname.connects stream fsIn to the external file fname. fsIn.get(char& character) fsIn.get(char& character) extracts next character from the input stream fsIn and places it in the character variable character.extracts next character from the input stream fsIn and places it in the character variable character. fsIn.eof() fsIn.eof() tests for the end-of-file condition.tests for the end-of-file condition.

More Output File-Related Functions ofstream fsOut; ofstream fsOut; fsOut.open(const char[] fname) fsOut.open(const char[] fname) connects stream fsOut to the external file fname.connects stream fsOut to the external file fname. fsOut.put(char character) fsOut.put(char character) inserts character character to the output stream fsOut.inserts character character to the output stream fsOut. fsOut.eof() fsOut.eof() tests for the end-of-file condition.tests for the end-of-file condition.

File Open Mode NameDescription ios::inOpen file to read ios::outOpen file to write ios::appAll the date you write, is put at the end of the file. It calls ios::out ios::ateAll the date you write, is put at the end of the file. It does not call ios::out ios::truncDeletes all previous content in the file. (empties the file) ios::nocreateIf the file does not exists, opening it with the open() function gets impossible. ios::noreplaceIf the file exists, trying to open it with the open() function, returns an error. ios::binaryOpens the file in binary mode.

File Open Mode #include int main(void) { ofstream outFile("file1.txt", ios::out); outFile << "That's new!\n"; outFile.close(); Return 0; } If you want to set more than one open mode, just use the OR operator- |. This way: ios::ate | ios::binary

Dealing with Binary files Functions for binary file handling Functions for binary file handling get(): read a byte and point to the next byte to read get(): read a byte and point to the next byte to read put(): write a byte and point to the next location for write put(): write a byte and point to the next location for write read(): block reading read(): block reading write(): block writing write(): block writing

Dealing with Binary files Some useful functions Some useful functions seekg(): Go to a specific position when reading seekg(): Go to a specific position when reading seekp(): Go to a specific position when writing seekp(): Go to a specific position when writing tellg() : Retunrs an int type, that shows the current position of the inside-pointer. This one works only when you read a file. tellg() : Retunrs an int type, that shows the current position of the inside-pointer. This one works only when you read a file. tellp(): The same as tellg() but used when we write in a file. tellp(): The same as tellg() but used when we write in a file. flush(): Save data from the buffer to the output file. flush(): Save data from the buffer to the output file.

Binary File I/O Examples // Example 1: Using get() and put() #include void main() { fstream File("test_file",ios::out | ios::in | ios::binary); char ch; ch='o'; File.put(ch); //put the content of ch to the file File.seekg(ios::beg); //go to the beginning of the file File.get(ch); //read one character cout << ch << endl; //display it File.close(); }

Binary File I/O Examples //Example 2: Using read() and write() #include #include void main() { fstream File("test_file.txt",ios::out | ios::in | ios::binary); char arr[13]; strcpy(arr,"Hello World!"); //put Hello World! into the array File.write(arr,5); //put the first 5 symbols into the file- "Hello" File.seekg(ios::beg); //go to the beginning of the file static char read_array[10]; //I will put the read data, here File.read(read_array,3); //read the first 3 symbols- "Hel" cout << read_array << endl; //display them File.close();}

More Binary File I/O Examples #include void main() { //if we have "Hello" in test_file.txt ifstream File("test_file.txt"); char arr[10]; File.read(arr,10); //this should return 5, as Hello is 5 characters long cout << File.tellg() << endl; File.close(); }

Summary of Input File-Related Functions #include #include ifstream fsIn; fsIn.open(const char[] fname) fsIn.open(const char[] fname) connects stream fsIn to the external file fname.connects stream fsIn to the external file fname. fsIn.get(char& c) fsIn.get(char& c) extracts next character from the input stream fsIn and places it in the character variable c.extracts next character from the input stream fsIn and places it in the character variable c. fsIn.eof() fsIn.eof() tests for the end-of-file condition.tests for the end-of-file condition. fsIn.close() fsIn.close() disconnects the stream and associated file.disconnects the stream and associated file. fsIn >> c; //Behaves just like cin fsIn >> c; //Behaves just like cin

Summary of Output File-Related Functions #include #include ofstream fsOut; fsOut.open(const char[] fname) fsOut.open(const char[] fname) connects stream fsOut to the external file fname.connects stream fsOut to the external file fname. fsOut.put(char c) fsOut.put(char c) inserts character c to the output stream fsOut.inserts character c to the output stream fsOut. fsOut.eof() fsOut.eof() tests for the end-of-file condition.tests for the end-of-file condition. fsOut.close() fsOut.close() disconnects the stream and associated file.disconnects the stream and associated file. fsOut << c; //Behaves just like cout fsOut << c; //Behaves just like cout