0% found this document useful (0 votes)
3 views2 pages

OOPp 4

Uploaded by

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

OOPp 4

Uploaded by

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

#include <iostream>

#include <fstream>

using namespace std;

int main()

// Step 1: Create an output file and write information to it

ofstream outf("example.txt");

if (!outf)

cerr << "Error opening file for writing!" << endl;

return 1;

outf << "Hello, World!" << endl;

outf << "This is a simple file I/O example in C++." << endl;

outf << "Goodbye!" << endl;

outf.close(); // Close the output file

// Step 2: Open the file again as an input file and read the information

ifstream inf("example.txt");

if (!inf)

cerr << "Error opening file for reading!" << endl;

return 1;

char line[256]; // Character array to hold each line

while (inf.getline(line, sizeof(line)))

cout << line << endl; // Output each line read from the file
}

inf.close(); // Close the input file

return 0;

OUTPUT

You might also like