File IO in C++ (ppt)

57 %
43 %
Information about File IO in C++ (ppt)
Science-Technology

Published on February 25, 2008

Author: sagib

Source: authorstream.com

File I/O in C++: File I/O in C++ Using Input/Output Files : Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; 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; must be opened before it is used. General File I/O Steps: 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 : Using Input/Output Files stream - a sequence of characters interactive (iostream)  cin - input stream associated with keyboard.  cout - output stream associated with display. 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: Stream I/O Library Header Files Note: There is no “.h” on standard header files : <fstream> iostream -- contains basic information required for all stream I/O operations iomanip -- contains information useful for performing formatted I/O with parameterized stream manipulators 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) PowerPoint Presentation: ios is the base class. istream and ostream inherit from ios ifstream inherits from istream (and ios) ofstream inherits from ostream (and ios) iostream inherits from istream and ostream (& ios) fstream inherits from ifstream, iostream, and ofstream Classes for Stream I/O in C++ PowerPoint Presentation: C++ streams #include <fstream> int main (void) { //Local declarations ifstream fsIn; ofstream fsOut; . . . return 0; } Object and Member Functions : 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: File I/O Example: Writing #include <fstream> using namespace std; int main(void) { ofstream outFile(“fout.txt"); outFile << "Hello World!"; outFile.close(); return 0; } File I/O Example: Writing: File I/O Example: Writing #include <fstream> using namespace std; int main(void) { ofstream outFile; outFile.open(“fout.txt”); outFile << “First line”; //behave just like cout outFile.close(); outFile<<“Another line”<<endl; //?? return 0; } File I/O Example: Reading: File I/O Example: Reading #include <iostream> #include <fstream> 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; } File I/O Example: Reading: File I/O Example: Reading #include <iostream> #include <fstream> #include <string> 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; } File I/O Example: Reading: File I/O Example: Reading #include <iostream> #include <fstream> #include <string> int main(void) { ifstream openFile(“data.txt"); //open a text file data.txt string line; 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{ cout<<“File does not exist!”<<endl; exit(1);} } openFile.close(); return 0; } More Input File-Related Functions : More Input File-Related Functions ifstream fsin; fsIn.open( const char [] fname) connects stream fsIn to the external file fname. fsIn.get( char & character) extracts next character from the input stream fsIn and places it in the character variable character. fsIn.eof() tests for the end-of-file condition. More Output File-Related Functions : More Output File-Related Functions ofstream fsOut; fsOut.open( const char [] fname) connects stream fsOut to the external file fname. fsOut.put( char character) inserts character character to the output stream fsOut . fsOut.eof() tests for the end-of-file condition. File Open Mode: File Open Mode Name Description ios::in Open file to read ios::out Open file to write ios::app All the date you write, is put at the end of the file. It calls ios::out ios::ate All the date you write, is put at the end of the file. It does not call ios::out ios::trunc Deletes all previous content in the file. (empties the file) ios::nocreate If the file does not exists, opening it with the open() function gets impossible. ios::noreplace If the file exists, trying to open it with the open() function, returns an error. ios::binary Opens the file in binary mode. File Open Mode: File Open Mode #include <fstream> 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: Dealing with Binary files Functions for binary file handling get(): read a byte and point to the next byte to read put(): write a byte and point to the next location for write read(): block reading write(): block writing Dealing with Binary files: Dealing with Binary files Some useful functions seekg(): Go to a specific position when reading 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. tellp(): The same as tellg() but used when we write in a file. flush(): Save data from the buffer to the output file. Binary File I/O Examples: Binary File I/O Examples // Example 1: Using get() and put() #include <iostream> #include <fstream> 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: Binary File I/O Examples //Example 2: Using read() and write() #include <fstream.h> #include <string.h> 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: More Binary File I/O Examples #include <fstream> 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 : Summary of Input File-Related Functions #include <fstream> ifstream fsIn; fsIn.open( const char [] fname) connects stream fsIn to the external file fname. fsIn.get( char & c) extracts next character from the input stream fsIn and places it in the character variable c. fsIn.eof() tests for the end-of-file condition. fsIn.close() disconnects the stream and associated file. fsIn >> c; //Behaves just like cin Summary of Output File-Related Functions : Summary of Output File-Related Functions #include <fstream> ofstream fsOut; fsOut.open( const char [] fname) connects stream fsOut to the external file fname. fsOut.put( char c) inserts character c to the output stream fsOut . fsOut.eof() tests for the end-of-file condition. fsOut.close() disconnects the stream and associated file. fsOut << c; //Behaves just like cout

Add a comment

Related presentations

Related pages

Intro to File Input/Output in C - Computer Science ...

There are types and functions in the library stdio.h that are used for file I/O. Make sure you always include that ... Intro to File Input/Output in C
Read more

File I/O in C - Electrical and Computer Engineering

Times New Roman Arial Courier New 1_Blank Presentation Microsoft Clip Gallery File I/O in C Files in C Files in C Opening Files Testing for Successful ...
Read more

File Operation in C - YouTube

http://vinodthebest.wordpress.com/ In this video I have explained about File I/O Operation in C: Part 1: 1. Explain File I/O & its need. 2 ...
Read more

PowerPoint Presentation - Bjarne Stroustrup's Homepage

... Integer Floating point File modes Binary I/O Positioning String ... a b c d e f // simple test ... Chapter 11 Customizing I/O Overview Kinds of I/O ...
Read more

Chapter 9 Streams and File I/O - Home - Florida Tech ...

Streams and File I/O ... 012 146 141 143 145 163 e y n f a c e s 0000020 012 n ... be written to a binary file. Chapter 9 ...
Read more

File (Java Platform SE 7 ) - Oracle Help Center

Where it is required to distinguish an I/O exception from the case that the file is not a normal ... on Microsoft Windows systems it is typically "C: ...
Read more

How to do basic file I/O in Visual C#

How to do basic file I/O in Visual C# ... ("File Written to C:\KBTest.txt"); } private void button3_Click(object sender, System ...
Read more

Basics of I/O Streams and File I/O

I/O Streams and File I/O Basics of I/O Streams and File I/O ... You cannot give fin.open() an argument that is a C++ string; you must give it a C string.
Read more

Introduction to the C Programming Language

... additional local and external functions and variables A Simple C Program Create example file: try.c ... file and console (also a file) IO: perror ...
Read more