Header Ads Widget

Ticker

6/recent/ticker-posts

Practical 15 OOP

 PRACTICAL – 15.1

Write a program to create files with constructor function, open function, and using various file mode parameters.

#include <iostream>
#include <conio.h>
#include <string.h>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main()
{

    char fname[20];
    char rec[80], ch;

    cout << "Enter file name: ";
    cin.get(fname, 20);

    fstream fout(fname, ios::out);

    if (!fout)
    {
        cout << "Error in opening the file " << fname;
        getch();
        exit(1);
    }
    cin.get(ch);

    cout << "\nEnter a line to store in the file:\n";
    cin.get(rec, 80);
    fout << rec << "\n";
    cout << "\nThe entered line stored in the file successfully..!!";
    cout << "\nPress any key to see...\n";
    getch();
    fout.close();

    fstream fin(fname, ios::in);
    if (!fin)
    {
        cout << "Error in opening the file " << fname;
        cout << "\nPress any key to exit...";
        getch();
        exit(2);
    }

    cin.get(ch);
    fin.get(rec, 80);
    cout << "\nThe file contains:\n";
    cout << rec;
    cout << "\n\nPress any key to exit...\n";
    fin.close();

    getch();
    return 0;
}

PRACTICAL –  15.2

Write a program to use the following functions: Seekg(), Tellg(), Seekp(), Tellp(), Put(), Get(), Write(), Read()

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class jp
{
    int age;
    char *name = new char[100];

public:
    void ta_ipt()
    {
        cin.sync();
        cout << "Enter The Name: ";
        cin.get(name, 100, '\n'); //--------------->Use of get()
        cout << " " << endl;
        cout << "Enter The Age: ";
        cin >> age;
        cout << "Given name is: " << endl;
        while (*name != '\0')
        {
            cout.put(*name); ///---------------->Use of put()
            name++;
        }
    }
};

int main()
{

    fstream inOut("abc.txt",
                  fstream::ate | fstream::in | fstream::out);
    if (!inOut)
    {
        cerr << "Unable to open file!" << endl;
        return EXIT_FAILURE;
    }

    auto end_mark = inOut.tellg();
    inOut.seekg(0, fstream::beg);
    size_t cnt = 0;
    string line;

    while (inOut && inOut.tellg() != end_mark && getline(inOut, line))
    {
        cnt += line.size() + 1;
        auto mark = inOut.tellg();
        inOut.seekp(0, fstream::end);
        inOut << cnt;

        if (mark != end_mark)
            inOut << " ";
        inOut.seekg(mark);
    }
    inOut.seekp(0, fstream::end);
    inOut << "\n";

    // Writing
    jp ipt;
    fstream w_obj("abc.txt", ios::app | ios::binary | ios::in);
    w_obj.seekp(0, ios::end);
    ipt.ta_ipt();
    w_obj.write((char *)&ipt, sizeof(jp)); //------------->Use of Write() for writing for print computer readable binary characters in given textfile
    w_obj.read((char *)&ipt, sizeof(jp));  //------>Use of Read() for Assigning Address

    return 0;
}

Post a Comment

0 Comments