0% found this document useful (0 votes)
2 views

File

The document provides an overview of file input/output (I/O) operations in C++, detailing the steps for using files, the types of file streams, and important functions like open() and getline(). It explains the necessity of including the fstream header, the declaration of file stream variables, and the process of opening, using, and closing files. Additionally, it includes code examples demonstrating how to read from and write to files.

Uploaded by

randsand97
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

File

The document provides an overview of file input/output (I/O) operations in C++, detailing the steps for using files, the types of file streams, and important functions like open() and getline(). It explains the necessity of including the fstream header, the declaration of file stream variables, and the process of opening, using, and closing files. Additionally, it includes code examples demonstrating how to read from and write to files.

Uploaded by

randsand97
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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
o provide input data to a program
o or receive output data from a program
o or both;
 should reside in Project directory for easy access;
 must be opened before it is used

General File I/O Steps


1. Include the header file fstream in the program.
2. Declare file stream variables.
3. Associate the file stream variables with the input/output files by
opening the file.
4. Use the file stream variables with >>, <<, or other input/output
functions.
5. Close the file.

Streams
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).

-------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara ………………. 1 ………..………. Computer Programming
Streams

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
 fstream -- contains information for performing file I/O operations

File Stream Objects


• There are three types of file stream objects
(1) ifstream objects: used for input
(2) ofstream objects: used for output
(3) fstream objects: used for both input and output

C++ streams

-------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara ………………. 2 ………..………. Computer Programming
Open() Function
 Opening a file associates a file stream variable declared in the
program with a physical file at the source, such as a disk.
 In the case of an input file:
o the file must exist before the open statement executes.
o If the file does not exist, the open statement fails and the
input stream enters the fail state
 An output file does not have to exist before it is opened;
o if the output file does not exist, the computer prepares an
empty file for output.
o If the designated output file already exists, by default, the
old contents are erased when the file is opened.

Open() Function

Important File Functions


getline() - read a line of text from text file store in a string.
Syntax: getline(Input file, String, Delimiting Char);

Example: getline(ifile1, str1, ‘,’);


Example: getline(ifile1, str1);

eof() - This function determines the end-of-file by returning


true(non-zero) for end of file otherwise returning false(zero).

Example : fout.eof( );

-------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara ………………. 3 ………..………. Computer Programming
Example1: Opening and Closing
#include <fstream> int main ()
{
ifstream fsIn; //input
ofstream fsOut; // output //Open the files
fsIn.open("prog1.txt"); //open the input file
fsOut.open("prog2.txt"); //open the output
//Code for data manipulation

//Close files
fsIn.close();
fsOut.close();
return 0;
}

Example2: Writing a string to a file


#include <fstream>
using namespace std;
int main()
{ // declare output file variable
ofstream outFile;
// open an exist file fout.txt
outFile.open("fout.txt”);
//behave just like cout,
//put the word into the file
outFile << "Hello World!";
// Closing the file
outFile.close();
return 0;
}

-------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara ………………. 4 ………..………. Computer Programming
Example3: Reading from a file
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line1;
ifstream myfile;
myfile.open("example.txt");
if (myfile.is_open()) {
while (getline(myfile, line1)) {
cout << line1 << '\n'; }
myfile.close();
}
else
cout << "Unable to open file"<< endl;
system("pause");

return 0;
}

-------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara ………………. 5 ………..………. Computer Programming

You might also like