Ad

C++ File Handling

file handling

C++ File Handling is used to store data on a file. A file is a collection of related data stored in the disk. Programs can be designed to perform the read and write operations on these files.

Advertisement

Example 1: Input string and write in a file 1

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
    char a[20];
    clrscr();
    cout<<"Enter name";
    cin>>a;
    ofstream out("file.txt");
    out<<a;
    out.close();
    getch();
}

Example 2: Read contents from a file

#include<iostream.h>
#include<conio.h>
#include<fstream.h>

void main()
{
    char a[20];
    clrscr();
    ifstream in("file.txt");
    in>>a;
    cout<<a;
    in.close();
    getch();
}

Example 3: Write the text in a file then Read its contents. 1

#include<iostream.h>
#include<conio.h>
#include<fstream.h>

void main()
{
    char a[20], b[20];
    clrscr();
    cout<<"Enter name";
    cin>>a;
    ofstream out("file.txt");
    out<<a;
    out.close();
    ifstream out2("file.txt");
    out2>>b;
    cout<<b;
    out2.close();
    getch();
}

ifstream class contains following functions:

  • get() use to read a single character.
  • getline() use to read single line.
  • read() use to read single record.
  • seekg() Moves get pointer to a specified location
  • tellg() Tell the current position of get pointer

ofstream class contains following functions:

  • put() use to write a single character.
  • write() use to write a single record.
  • seekp() Moves put pointer to a specified location
  • tellp() Tell the current position of put pointer

Example 4: Input file name and display its contents if exists using getline function. 1

#include<iostream.h>
#include<conio.h>
#include<fstream.h>

void main()
{
    char a[20], b[20];
    clrscr();
    cout<<"Enter filename";
    cin>>a;
    cout<<"File Contents are\n";
    ifstream f;
    f.open(a);
    while(f)
    {
        f.getline(b,80);
        cout<<b;
    }
    getch();
}
S.No Seek Function Description
Fout.seekg(0,ios::beg)
or
Fout.seekg(0)
Go to begining of the file
Fout.seekg (m,ios::cur) Move m+1th byte in the file
Fout.seekg (0,ios::end) Go to end of file
S.No File Mode Description
ios::in Open file in Read Mode
ios::out Open file in Write Mode
ios::app Append at end of file
ios::ate Go to end of file
ios::binary Binary File
ios::trunc Delete contents if exists

Example 5: Read and write 5 students record in a file using structure. 1

#include<stdio.h>
#include<conio.h>
#include<fstream.h>

struct student
{
    int rollno;
    char name[20];
    int hindi, english, maths, science, sstudies;
    int total;
    float per;
}s[5], s2[5];

int main()
{
    int i;
    FILE *fp;
    clrscr();
    fp=fopen("student.dat","a");
    clrscr();
    for(i=0;i<4;i++)
    {
        printf("Enter student rollno, name and hindi, english, maths, science, sstudies");
        scanf("%d%s%d%d%d%d%d", &s[i].rollno, &s[i].name, &s[i].hindi, &s[i].english, &s[i].maths, &s[i].science, &s[i].sstudies);
        s[i].total=s[i].hindi+s[i].english+s[i].maths+s[i].science+s[i].sstudies;
        s[i].per=s[i].total/5;
        fwrite(&s[i],sizeof(s[i]), 1, fp);
    }
    fclose(fp);

    fp=fopen("student.dat","r");
    printf("Student info:\n");
    for(i=0;i<4;i++)
    {
        fread(&s2[i],sizeof(s2[i]),1,fp);
        printf("Roll no.%d\nName:%s\n", s2[i].rollno, s2[i].name);
        printf("Hindi marks:%d\nEnglish marks:%d\nMaths marks:%d\nScience marks:%d\n;S.Studies marks:%d\n",s2[i].hindi,s2[i].english,s2[i].maths,s2[i].science,s2[i].sstudies);
        printf("\nTotal:%d",s2[i].total);
        printf("\nPercentage:%.2f",s2[i].per);
    }
    fclose(fp);
    getch();
}

C Language Feedback, Questions, Suggestions, Discussion.

Ad: Do you know Njmcdirect Ticket on njmcdirect.com? What happens when you get a traffic offense ticket? Go to the municipal court and queue to pay. In New Jersey, things are easier no. you can visit the www. Njmcdirect .Com portal and make your payment. It is easy, first and reliable. All you have to do is have the things the site needs and the above steps to proceed.