Creating or opening a file
For writing data
Text Files
ofstream out (“myfile.txt”);
or
ofstream out;
out.open(“myfile.txt”);
Binary Files
Ofstream out (“myfile.txt”,ios::binary);
or
ofstream out;
out.open(“myfile.txt”, ios::binary);
For Appending (adding text at the end of the existing file)
Text Files
ofstream out(“myfile.txt”,ios::app);
or
ofstream out;
out.open(“myfile.txt”, ios::app);
Binary Files
Ofstream out
(“myfile.txt”,ios::app|ios::binary);
or
ofstream out;
out.open(“myfile.txt”, ios::app | ios::binary);
For reading data
Text Files
ifstream in (“myfile.txt”);
or
ifstream in ;
in.open(“myfile.txt”);
Binary Files
ifstream in (“myfile.txt”, ios::binary);
or
ifstream in ;
in.open(“myfile.txt”, ios::binary);



































Closing Files (after reading or writing)
ofstream object “out”
Ifstream object “in”
out.close();
in.close();
Reading / Writing Data to and from files
Data
Functions for reading file
Function for writing
into file
char
get();
put();
1
word
>>
(extraction
operator)
<< (insertion
operator)
>=1
word
getline();
<< (insertion
operator)
Objects
read()
write()
Binary
data
Same as
above
Same as
above
4) Functions that can be used to perform special tasks
Operation
function
Description
Checking end of
file.
eof()
Used to check eof during the reading of file
Check if an operation
fails.
bad()
Returns true if a reading or writing operation fails.
Check if an operation
fails.
Fail()
Returns true in the same cases as bad(), but also in the case that a format error happens.
Checking for opened
file.
is_open();
Checks if the file is opened or not, returns true if the file is opened else false
Number of bytes already
read.
gcount()
Returns count of the bytes read from the file
Ignoring characters
during file read.
ignore()
Ignores n bytes from the file. (get pointer is positioned after n character)
Checking next
character.
peek()
Checks the next available character, will not increase the get pointer to next character.
Random access (only for
binary files).
seekg()
seekp()
tellg()
tellp()
In case of binary files random access is performed using these functions. They either give or set the position of get and put pointers on the particular locatio

Posted by Unknown On 01:17 No comments

0 comments:

Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Blog Archive

Contact Us


Name

E-mail *

Message *