File Handling Rao
File Handling Rao
File Handling Rao
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;
ofstream ofs("StudentRecord.txt");
// ofstream ofs;
// ofs.open("StudentRecord.txt");
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");
ifs.close();
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.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;
}
ofstream ofs(s);
if(!ofs)
{
cout << "\nUnable to open output file..." << s;
return 1;
}
char 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();
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; }
};
int main()
{
Student s;
ofstream ofs("S.txt",ios::binary);
if(!ofs)
{
cout << "\nUnable to open a file...";
return 1;
}
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 << "\nName: " << s.getName() << endl << "Marks: ";
int *m = s.getMarks();
for (int j = 1; j <= 5; j++ )
cout << m[j-1] << '\t';
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; }
};
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; }
};
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));
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;
}
fs.seekp(pos-1);
fs.put(c);
fs.close();
return 0;
}