• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

File concepts in C++

Files in C++

File is a named memory location in secondary storage.

File is collection of data stored permanently with in disk.

Of stream (output file stream calss)

Output file stream class.

This class opens the file in output Mode.

Always output mode used for writing.

 

Hierarchy of of stream class

Screenshot_35

 

File Constructor

osstem (char *fname)

  • This constructor create osstream class object with given file name.
  • ostream(char*fname,int mode)
  • This constructor create ostrem class object with give filename and mode.

ios::out -this create new file

ios:: app-this open existing file for appending.

 

//program to create marks file to store student marks details

# include <iostream.h>

# include <fstream.h>

void Main()

{

char name[10];

int sub1,sub2;

ofstrem fout (“ c:\\Marks”);

char ch;

clrscr();

cout<<”\n Input name”;

cin>>name;

cout<<”\n Input two subjects”;

cin >>sub1>>sub2;

fout<<name<<endl;

fout<<sub1<<endl;

fout<<sub2<<endl;

cout<<”\n Add another student?”;

cin >>ch;

}

while(ch!=’n’);

}

 

 

ifstream (input file stream class in C++)          

  • input file stream class
  • This class is used to read data from existing file.
  • it opens the file in input mode(reading the file).

 

Constructor

  • ifstream (char *fname)
  • This constructor create if stream class object with given filename.
  • It opens the given file name in input mode.

 

Program to read contents of file in C++

# include <iostream.h>

#include <conio.h>

# include <fstream.h>

void main()

{

char name[10];

int sub1,sub2;

ifstream fin

clrscr();

while(1)

{

fin >>name;

fin >>sub1>>sub2;

if (fin.eof ())

break;

cout<<endl<<name<<”\t<<sub1<<”\t”<<sub2;

}

 

 Writing Object inside file

Screenshot_36

 

 

# include <iostrem.h>

# include<fstream.h>

class address

{

char name[10];

char street[10];

char city[10];

public:

void read_address()

{

cout<<”\n Input name “;

cin>>name;

cout<<”\n Input street”;

cin>> street;

cout<<”\n Input city”;

cin>> city;

}

void print_address()

{

cout<<endl<<name<<”\t”<<street<<”\t”<<city;

}};

void main()

{

address add;

char ch;

ofstream fout (“c://address_book”);

do{

add.read_address()

fout.write((char*)&add,size of (add));

cout<<”\n Add another book?”;

cin>>ch;

}

while (ch!=’n’)

fout.close();

}

 

 

//Reading address from address book/reading objects from file

# include <iostream.h>

# include<conio.h>

#include<fstream.h>

class address

{

char name[10];

char street[10];

char city [10];

public :

void  read_address()

{

cout<<”\n Input Name”;

cin >>Name;

cout<<”\n Input Street”;

cin>>street;

cout<<”\n Input city”;

cin >>city;

}

void print_address()

{

cout <<endl<<Name<<”\t”<<street<<”\t”<<city;

}};

 

void main()

{

address add;

ifstream fin(“c:\\address_book”);

while(1)

{

fin .read((char*)&add,sizeof(add));

if (fin,eof());

break;

add.print_address();

}

fin.close();

}

 

 

MANIPULATION OF FILE POINTERS in C++

 

We can move the file pointer to any desired position in the file. We can take the control of the file pointer.

The functions to position the file pointer at desired position.

seekg(offset,position);seekp(offset,position);

offset parameter represents number of bytes and position parameter takes one of the following 3 constants;

 

ios::beg ios :: cur ios:: end

Screenshot_37

fin.seekg(4,ios::beg);

fin.seekg(2, ios::beg);

fin.seekg(3,ios::cur);

fin.seekg(-4,ios::cur);

cout<<fin.tellg(); à

fin.seekg(-2,ios::end); => (7-2)=>5 offset position

Reading Address randomly:

fin.seekg(addno*sizeof(add),ios ::beg);

void main()

{

address add;

int addno;

ifstream fin(“c:\\address_book”);

cout<<”\n Input address number”;

cin>>adddno;

fin.seekg(addno*sizeof (add),ios::beg);

fin read((char*)&add,size of (add));

if fin.eof())

cout<<”\n Invalid address number”;

else

add print_address();

}

 

Summary
Review Date
Reviewed Item
File concepts in C++

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on File concepts in C++"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.