0% found this document useful (0 votes)
6 views8 pages

File Handling in C

Uploaded by

pandeyridhesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

File Handling in C

Uploaded by

pandeyridhesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

File handling in C++

Ridhesh Mani Pandey


Divyam Jha
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
Why to use Files
Convenient way to deal large quantities of data.
Store data permanently (until file is deleted).
Avoid typing data into program multiple times.
Share data between programs.
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 of bytes to do input and output to different devices.
• Stream is the basic concepts which can be attached to files, strings, console
and other devices.
• User can also create their own stream to cater specific device or user defined
class.
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

flush():Save data from the buffer to the output file.


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: 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;
}

You might also like