0% found this document useful (0 votes)
17 views10 pages

BPLCK205D Module 4 Programs

The document provides an introduction to file handling in C++ programming, covering methods for reading and writing text and binary files using various functions such as >>, <<, get(), put(), and getline(). It includes sample programs demonstrating how to open files, read user input, and write to files, as well as how to handle binary files with structures. Additionally, it highlights the limitations of certain input methods regarding whitespace and line breaks.

Uploaded by

Antheesh R
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)
17 views10 pages

BPLCK205D Module 4 Programs

The document provides an introduction to file handling in C++ programming, covering methods for reading and writing text and binary files using various functions such as >>, <<, get(), put(), and getline(). It includes sample programs demonstrating how to open files, read user input, and write to files, as well as how to handle binary files with structures. Additionally, it highlights the limitations of certain input methods regarding whitespace and line breaks.

Uploaded by

Antheesh R
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/ 10

BPLCK205D-Introduction to C++ programming – File Handling

Text file handling

Opening file for performing read, write operations using >> and <<

operators respectively.

C++ program to open file using constructor of class ofstream/ifstream.

This program opens a file “abc.txt” in output(write) mode by creating

object EntryFile for class ofstream and read the data from keyboard using

cin>> till the word End entered by user and the same time the entered

data from the keyboard is written to the file “abc.txt” using EntryFile<<.

Again opens the file “abc.txt” in input(read) mode by creating object

DisplayFile for the class ifstream then read the data from the file using

DisplayFile >> and write to the screen using cout<<.

Note: the operator >> will not take white space as input character so the

words entered from keyboard written to the file continuously without

space.

This problem will be solved using get( ) and put( ) functions.

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

int main()

Dr. Antheesh R, Associate Professor, Dept. of ADE, SDMIT


Ujire
BPLCK205D-Introduction to C++ programming – File Handling

string InputLine, OutputLine;

ofstream EntryFile("abc.txt"); // opening file “abc.txt” if already exist

//contents will be erased.

while(true)

cin >> InputLine; // reading data from keyboard

if(InputLine == "End") // if word from keboard is “End” then

//stops reading and writing to file.

break;

EntryFile << InputLine; // writing to the file “abc.txt”

EntryFile.close(); // closing file “abc.txt”

ifstream DisplayFile("abc.txt"); // opening file “abc.txt” in

//input/read mode

while(DisplayFile.eof()==0)

DisplayFile >> OutputLine; // rading data from the file “abc.txt”

cout << OutputLine << "\n"; // displaying the data on the screen

DisplayFile.close();

return 0;

Dr. Antheesh R, Associate Professor, Dept. of ADE, SDMIT


Ujire
BPLCK205D-Introduction to C++ programming – File Handling

Opening file for performing read, write operations using get() and

put() functions respectively.

C++ program to open file using constructor of class ofstream/ifstream.

This program opens a file “xyz.dat” in output(write) mode by creating

object WriteFile for class ofstream and read the data from keyboard using

cin.get() function till the character $ is entered by user and the same

time the entered data from the keyboard is written to the file “xyz.dat”

using cou.put() function.

Again opens the file “xyz.dat” in input(read) mode by creating object

ReadFile for the class ifstream then read the data from the file using

ReadFile.get() function and write to the screen using cout.put() function.

include <iostream>

#include <string>

#include <fstream>

using namespace std;

#include <iomanip>

int main()

char ch;

ofstream WriteFile("xyz.dat");

cout<<"Enter the data to write to file\n";

Dr. Antheesh R, Associate Professor, Dept. of ADE, SDMIT


Ujire
BPLCK205D-Introduction to C++ programming – File Handling

while(true)

cin.get(ch);

if(ch == '$')

break;

WriteFile.put(ch);

WriteFile.close();

cout<<"\n";

ifstream ReadFile("xyz.dat");

while(ReadFile.eof()==0)

ReadFile.get(ch);

cout.put(ch);

ReadFile.close();

cout<<"\n";

return 0;

Dr. Antheesh R, Associate Professor, Dept. of ADE, SDMIT


Ujire
BPLCK205D-Introduction to C++ programming – File Handling

Using getline() function for reading from keyboard and file.

cin.getline() function reads a line at a time from keyboard and stores in

character array.

file_stream_object.getline() function reads a line at a time and stores in

character array.

Note: When we read a string in a file, the file does not take next line

character as input. Thus, pressing the <Enter> key terminates the string

entry, but the file will not contain that <Enter>. When the file is read back,

we will not be able to see the output in different lines.

#include <iostream>

#include <cstring>

#include <fstream>

using namespace std;

int main()

char InputLine[80], OutputLine[80];

ofstream EntryFile("FewLines.dat");

while(true)

cin.getline(InputLine, 80);

if(!strcmp(InputLine, "End"))

break;

Dr. Antheesh R, Associate Professor, Dept. of ADE, SDMIT


Ujire
BPLCK205D-Introduction to C++ programming – File Handling

EntryFile << InputLine;

EntryFile.close();

ifstream DisplayFile("FewLines.dat");

while(!DisplayFile.eof())

DisplayFile.getline(OutputLine, 80);

cout << OutputLine;

DisplayFile.close();

return 0;

Handling Binary Files

C++ program to create/open binary file in output/write mode and

write data to the created/opened binary file.

#include <iostream>

#include <fstream>

using namespace std;

struct student

int RollNo;

Dr. Antheesh R, Associate Professor, Dept. of ADE, SDMIT


Ujire
BPLCK205D-Introduction to C++ programming – File Handling

char Name[30];

char Address[40];

};

void ReadStudent(student & TempStud)

cout << "\n Enter roll no.: ";

cin >> TempStud.RollNo;

cout << "\n Enter name: ";

cin >> TempStud.Name;

cout << "\n Enter address: ";

cin >> TempStud.Address;

cout << "\n";

int main()

struct student E_Section;

ofstream PLC_Out;

PLC_Out.open("CPP.txt", ios::out | ios::binary | ios::trunc);

if(!PLC_Out.is_open())

cout << "File cannot be opened \n";

char Continue = 'y';

do

Dr. Antheesh R, Associate Professor, Dept. of ADE, SDMIT


Ujire
BPLCK205D-Introduction to C++ programming – File Handling

ReadStudent(E_Section);

PLC_Out.write((char*) &E_Section, sizeof(struct student));

if(PLC_Out.fail())

cout << "File write failed";

cout << "Do you want to continue? (y/n): ";

cin >> Continue;

} while(Continue != 'n');

PLC_Out.close();

return 0;

C++ program to open binary file in input/read mode and read data

from the opened binary file.

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

struct student

int RollNo;

Dr. Antheesh R, Associate Professor, Dept. of ADE, SDMIT


Ujire
BPLCK205D-Introduction to C++ programming – File Handling

char Name[30];

char Address[40];

};

void WriteStudent(student TempStud)

cout << "\n The roll no.: ";

cout << TempStud.RollNo;

cout << "\n The name: ";

cout << TempStud.Name;

cout << "\n The address: ";

cout << TempStud.Address;

cout << "\n";

int main()

struct student E_Section;

ifstream PLC_In("CPP.txt", ios::in | ios::binary);

while(!PLC_In.eof())

PLC_In.read((char*) &E_Section, sizeof(struct student));

if(PLC_In.fail())

break;

Dr. Antheesh R, Associate Professor, Dept. of ADE, SDMIT


Ujire
BPLCK205D-Introduction to C++ programming – File Handling

WriteStudent(E_Section);

PLC_In.close();

return 0;

Dr. Antheesh R, Associate Professor, Dept. of ADE, SDMIT


Ujire

You might also like