File Handling Rao

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

--------------------------------------------------------------------------------

Objective 1: Write a program to read student details from the user and write the same to a file.
--------------------------------------------------------------------------------

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
long rollNumber;
char *name;
int marks[5], i;

cout << "\nEnter roll number: ";


cin >> rollNumber;

cout << "\nEnter name: ";


cin >> name;

cout << "\nEnter marks...\n";


for ( i = 1; i <= 5; i++ )
{
cout << "Subject " << i << ": ";
cin >> marks[i-1];
}

ofstream ofs("StudentRecord.txt");
// ofstream ofs;
// ofs.open("StudentRecord.txt");

if(!ofs) // if(!ofs.is_open()) or if(ofs.fail())


{
cout << "\nUnable to open a file...";
return 1;
}

ofs << rollNumber << '\t' << name << '\t';


for ( i = 1; i <= 5; i++ )
ofs << marks[i-1] << '\t';

ofs.close();
return 0;
}
--------------------------------------------------------------------------------
Objective 2: Write a program to read student details from the file generated in Objective 1.
--------------------------------------------------------------------------------

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
long rollNumber;
char *name;
int marks[5], i;

ifstream ifs("StudentRecord.txt");
// ifstream ifs;
// ifs.open("StudentRecord.txt");

if(!ifs) // if(!ifs.is_open()) or if(ifs.fail())


{
cout << "\nUnable to open a file...";
return 1;
}

ifs >> rollNumber >> name;


for ( i = 0; i < 5; i++ )
ifs >> marks[i];

ifs.close();

cout << "\nRoll number: " << rollNumber;


cout << "\nName: " << name;
cout << "\nMarks...\n";
for ( i = 1; i <= 5; i++ )
cout << "Subject " << i << ": " << marks[i-1] << endl;

return 0;
}
--------------------------------------------------------------------------------
Objective 3: Write a C++ program to write number 1 to 100 in a data file 'Num.txt'.
--------------------------------------------------------------------------------

#include<iostream>
#include<fstream>
using namespace std;

int main()
{ ofstream ofs("Num.txt");
if(!ofs)
{ cout<<"Unable to open a file...";
return 1;
}
for(int i = 1; i <= 100; i++)
ofs << i << ' ';
ofs.close();
}

--------------------------------------------------------------------------------
Objective 4: Write a C++ program to append number 101 to 200 in a data file 'Num.txt' created in
Objective 3.
--------------------------------------------------------------------------------

#include<iostream>
#include<fstream>
using namespace std;

int main()
{ ofstream ofs("Num.txt",ios::app);
if(!ofs)
{ cout<<"Unable to open a file...";
return 1;
}
for(int i = 101; i <= 200; i++)
ofs << i << ' ';
ofs.close();*/
}
--------------------------------------------------------------------------------
Objective 5: Write a user-defined function in C++ to read the content from a text file, count and display
the number of alphabets present in it.
--------------------------------------------------------------------------------

#include<iostream>
#include<ctype.h>
#include<fstream>
using namespace std;

int countChar();

int main()
{
char s[100];
cout << "Enter message: ";
cin.get(s,100);

ofstream ofs("Message.txt");

if(!ofs)
{
cout << "\nUnable to open a file...";
return 1;
}

ofs << s;
ofs.close();
cout << "\nNumber of characters: " << countChar();
}

int countChar()
{
ifstream ifs("Message.txt");

if(!ifs)
{
cout << "\nUnable to open a file...";
return 1;
}

int c = 0;
char ch;

ifs >> ch;


while(ifs) // while(!ifs.eof())
{
if(isalpha(ch)) // isalpha(ch) returns true if ch is an alphabet.
c++;
ifs >> ch;
}

ifs.close();
return c;
}
--------------------------------------------------------------------------------
Objective 6: Write a program to read two filenames from the user: the source filename, say 'file1', and the
destination filename, say 'file2'. Read the contents from 'file1' and write them in 'file2', i.e.
copy 'file1' to 'file2'.
--------------------------------------------------------------------------------

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
char s[20];
cout << "Enter file name for input: ";
cin>>s;

ifstream ifs(s);
if(!ifs)
{
cout << "\nUnable to open input file..." << s;
return 1;
}

cout << "Enter file name for output: ";


cin>>s;

ofstream ofs(s);
if(!ofs)
{
cout << "\nUnable to open output file..." << s;
return 1;
}

char ch;

// Compact way of reading and writing a character.


while(ifs.get(ch))
ofs.put(ch); // ofs << ch;

ifs.close();
ofs.close();
return 0;
}
--------------------------------------------------------------------------------
Objective 7: Write a program to read and write a block of binary data (a complete array).
--------------------------------------------------------------------------------

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int a[] = {1,2,3,4,5,6,7,8,9,10};
char b[] = {'a','b','c','d','e','f','g','h','i','j'};

fstream fs;
fs.open("Arr",ios::binary | ios::out);
fs.write((char*) &a, sizeof(a));
fs.write((char*) &b, sizeof(b));
fs.close();

for(int i = 0; i < 10; i++)


{ a[i] = 0; b[i] = 'z'; }

fs.open("Arr", ios::in | ios::binary);


fs.read((char*) &a, sizeof(a));
fs.read((char*) &b, sizeof(b));

for(int i = 0; i < 10; i++)


cout << a[i] << " ";
cout << endl;

for(int i = 0; i < 10; i++)


cout << b[i] << " ";
fs.close();

return 0;
}
--------------------------------------------------------------------------------
Objective 8: Write a program to read and write a block of binary data (a class object).
--------------------------------------------------------------------------------

#include<iostream>
#include<fstream>
using namespace std;

class Student
{
long rollNumber;
char name[20];
int marks[5];

public:
void set();
long getRollNumber() { return rollNumber; }
char* getName() { return name; }
int* getMarks() { return marks; }
};

inline void Student :: set()


{
cout << "\nEnter roll number: ";
cin >> rollNumber;

cout << "\nEnter name: ";


cin >> name;

cout << "\nEnter marks...\n";


for (int i = 1; i <= 5; i++ )
{
cout << "Subject " << i << ": ";
cin >> marks[i-1];
}
}

int main()
{
Student s;

ofstream ofs("S.txt",ios::binary);
if(!ofs)
{
cout << "\nUnable to open a file...";
return 1;
}

for ( int i = 0; i < 3; i++ )


{ s.set();
ofs.write((char *) &s, sizeof(s));
}
ofs.close();

ifstream ifs("S.txt",ios::binary);

if(!ifs)
{
cout << "\nUnable to open a file...";
return 1;
}
while(true)
{
ifs.read((char *) &s, sizeof(s));

if(ifs.eof()) break;

cout << "-------------------\n";

cout << "Roll number: " << s.getRollNumber();

cout << "\nName: " << s.getName() << endl << "Marks: ";

int *m = s.getMarks();
for (int j = 1; j <= 5; j++ )
cout << m[j-1] << '\t';

cout << "\n-------------------\n";

ifs.close();

return 0;
}

--------------------------------------------------------------------------------
Objective 9: Write a program to calculate the file size in bytes.
--------------------------------------------------------------------------------

#include<iostream>
#include<fstream>
using namespace std;

int main ()
{
int beg,end;
ifstream fin("S.txt"); // specify any file exisiting in the folder.
beg = fin.tellg();
fin.seekg(0,ios::end);
end = fin.tellg();
fin.close();
cout << "File size is: " << (end-beg) << " bytes.\n";
return 0;
}
--------------------------------------------------------------------------------
Objective 10: Write a program to calculate total number of records in a file containing equal sized records.
--------------------------------------------------------------------------------

#include<iostream>
#include<fstream>
using namespace std;

class Student
{
long rollNumber;
char name[20];
int marks[5];

public:
void set();
long getRollNumber() { return rollNumber; }
char* getName() { return name; }
int* getMarks() { return marks; }
};

inline void Student :: set()


{
cout << "\nEnter roll number: ";
cin >> rollNumber;

cout << "\nEnter name: ";


cin.ignore();
cin.getline(name,20);

cout << "\nEnter marks...\n";


for (int i = 1; i <= 5; i++ )
{
cout << "Subject " << i << ": ";
cin >> marks[i-1];
}
}

int main()
{
fstream fin("S.txt",ios::in|ios::binary);
if(!fin)
{
cout << "\nUnable to open a file...";
return 1;
}

fin.seekg(0,ios::end);
cout << "Number of records: " << fin.tellg()/sizeof(Student);

fin.close();

return 0;
}
--------------------------------------------------------------------------------
Objective 11: Write a program to start printing from a specific record entered by the user.
--------------------------------------------------------------------------------

#include<iostream>
#include<fstream>
using namespace std;

class Student
{
long rollNumber;
char name[20];
int marks[5];

public:
void set();
long getRollNumber() { return rollNumber; }
char* getName() { return name; }
int* getMarks() { return marks; }
};

inline void Student :: set()


{
cout << "\nEnter roll number: ";
cin >> rollNumber;

cout << "\nEnter name: ";


cin.ignore();
cin.getline(name,20);

cout << "\nEnter marks...\n";


for (int i = 1; i <= 5; i++ )
{
cout << "Subject " << i << ": ";
cin >> marks[i-1];
}
}

int main()
{
Student s;
int i;
cout << "\nEnter the starting record number: ";
cin >> i;

fstream fin("S.txt",ios::in|ios::binary);

if(!fin)
{
cout << "\nUnable to open a file...";
return 1;
}

fin.seekg((i-1) * sizeof(s));

cout << "\nThe details of the students are as follows...\n";


while(fin.read((char *) &s, sizeof(s)))
{
cout << "-------------------\n";
cout << "Roll number: " << s.getRollNumber();
cout << "\nName: " << s.getName() << endl << "Marks: ";
int *m = s.getMarks();
for (int j = 1; j <= 5; j++ )
cout << m[j-1] << '\t';
cout << "\n-------------------\n";
}
fin.close();

return 0;
}
--------------------------------------------------------------------------------
Objective 12: Write a program to replace a character at a specific position with another character in the
file. The program takes 'position at which the character is replaced' and 'new character' as
input arguments.
--------------------------------------------------------------------------------

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
fstream fs("test.txt",ios::in|ios::out);

if(!fs)
{
cout << "\nUnable to open a file...";
return 1;
}

int pos; char c;


cout << "\nEnter position of a character to be updated: ";
cin >> pos;

cout << "\nEnter new character: ";


cin >> c;

fs.seekp(pos-1);
fs.put(c);
fs.close();

return 0;
}

You might also like