FILE
HANDLING CONCEPTS:
OS is the primary interface between the user and
the computer. Device driver is a small program that comes with every device IO
operations are performed with the help of the OS and the device driver. A C++
program does not directly request to the OS. It invokes a function from a
standard library that comes with a C++ compiler, when it is installed.
Text and Binary Files:
Text stream:
Ø deals
with information which is in ASCII form.
Ø if
<enter> key is pressed, Carriage return and Line Feed characters are
inserted. This is known as conversion. The text stream files can be opened by
any editor. Text files are more general.
Binary streams:
Ø binary
values are inserted
Ø no
conversion takes place.
The
binary stream files are restricted to the application that creates the file.
Binary files are not flexible.
Dealing With Text Files:
Ø ifstream
< filename> - read only file
Ø ofstream
<filename> - output /write only
file
Ø fstream <file name > -
both input/read and output /write file.
Manipulating Files Opening Files:
Files can
be opened using two methods
(i) using
constructors
(ii)
using open functions
File Modes:
When a
file is opened, it must be specified how it is to be opened. This means whether
to create it from new or overwrite it and whether it's text or binary, read or
write and if the content is to be appended to it.
In order
to open a file with a stream object open() member function is used. open
(filename, mode);
ios::ate
Write all
output to the end of file (even if file position pointer is moved with seekp)
ios::app
Open a
file for output and move to the end of the existing data (normally used to
append data to a file, but data can be written anywhere in the file
ios::in
The
original file (if it exists) will not be truncated
ios::out
Open a
file for output (default for ofstream objects)
ios::trunc
Discard
the file's contents if it exists (this is also the default action for ios::out,
if ios::ate, ios::app, or ios::in are not specified)
ios::binary
Opens the
file in binary mode (the default is text mode)
ios::nocreate
Open
fails if the file does not exist
ios::noreplace
Open
files if the file already exists.
Inserting
data somewhere in a sequential file would require that the entire file be
rewritten. It is possible, however, to add data to the end of a file without
rewriting the file. Adding data to the end of an existing file is called
appending.
fout.open("filename.dat",
ios::app) //open file for appending
Program to append to the contents of a file
#include
<iostream.h>
#include
<fstream.h>
int
main(void)
{
string name, dummy;
int number, i, age;
ofstream fout;
cout<<"How
many names do you want to add?";
cin>>number;
getline
(cin,dummy);
fout.open
("name_age.dat",ios::app); // open file for appending
assert
(!fout.fail( ));
for(i=1,
i<=number; i++)
{
cout<<"Enter
the name: ";
getline(cin,name);
cout<<"Enter
age: ";
cin>>age;
getline(cin,age);
fout<<name<<endl;
//send to file
fout<<age<<endl;
}
fout.close(
); //close file
assert(!fout.fail(
));
return 0;
}
Random Access:
In C++ ,
there are two types of file pointers to access a file in a random manner.
ü For a
file in the read mode – seekg (pointer for reading or getting)
ü For a
file in the write mode – seekp(pointer for wrioting or putting)
Using
these, it is possible to search any record of any file by skipping the other
records inbetween.
seekg() and seekp():
Ø seekg()
to move get or read pointer of file. It takes two arguments ,
§ number of
bytes to skip
§ from
where to skip
Ø seekp()
to move put and write pointers.
tellg() and tellp():
These
pointers tell us where the read and write pointers of a file are pointing to.
Ø tellg()
tells us where the get pointer is pointing to.
Ø tellp()
tells us where the put pointer is pointing to.
Example:
#include"stdafx.h"
#include<iostream>
#include<fstream>
struct
record
{
char
code[6];
char
name[20];
int i;
}r;
int main()
{
std::fstream
file("Temp.dat",std::ios::trunc|std::ios::in|std::ios::out|std::ios::binary);
if(!file)
{
std::cout<<"unable
to open file";
exit(0);
}
std::cout<<"enter
character code, name and an int\n";
std::cin.getline(r.code,6);
std::cin.getline(r.name,20);
std::cin>>r.i;
file.write((char
*)&r,sizeof(r));
std::cout<<"\n\n"<<file.tellg()<<'\n'<<file.tellp();
file.seekg(3);
std::cout<<"\n\n"<<file.tellg()<<'\n'<<file.tellp();
file.seekp(5);
std::cout<<"\n\n"<<file.tellg()<<'\n'<<file.tellp();
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.