Parameters.
s
A pointer to an array of characters.
n
The maximum number of characters to store, including the ternimating null character.
This is an integer value of type streamsize.
delim
The delimiter. The operation of extracting succesive characters is stopped when
delimiter is read. This parameter is optional, if not specified the function
considers '\n' the delimiter.
Return Value.
The function returns *this
Example.
// istream getline
#include <iostream>
using namespace std;
int main () {
char name[256], title[256];
cout << "Enter your name: ";
cin.getline (name,256);
cout << "Enter your favourite movie: ";
cin.getline (title,256);
cout << name << "'s favourite movie is " << title;
return 0;
}
This example demonstrates how to get line inputs from the standard input (
cin ).
Basic template member declarations ( basic_istream<charT,traits> ):
typedef charT char_type;
basic_istream& getline (char_type* s, streamsize n );
basic_istream& getline (char_type* s, streamsize n, char_type delim );
//////////////////////////////////////////////////////////////////////////
int get();
istream& get (char& c );
istream& get (char* s, streamsize n );
istream& get (char* s, streamsize n, char delim );
istream& get (streambuf& sb);
istream& get (streambuf& sb, char delim );
Extract unformatted data from stream.
These member functions perform unformatted input. The effect depends on the
variation used:
int get();
extracts a character from the stream and returns its value.
istream& get (char& c );
extracts a character from the stream and stores it in c.
istream& get (char* s, streamsize n );
istream& get (char* s, streamsize n, char delim );
extracts characters from the stream and stores them into successive locations
in the array pointed by s. Characters are extracted until either (n - 1) characters
have been extracted, the delimiter (parameter delim or '\n' if not specified)
is found, or if the end of file or any error occurs in the input sequence.
If the delimiter is found it is not extracted from the input stream and remains
as the next character to be extracted. Use getline if you want this character
to be extracted (and discarded).
An ending null character is automatically appended at the end of the content
stored in s.
istream& get (streambuf& sb);
istream& get (streambuf& sb, char delim );
extracts characters from the stream and inserts them in stream buffer sb. Characters
are extacted until either the delimiter (parameter delim or '\n' if not speciffied)
is found, or if the end of file or any error occurs in the input or output sequences.
Parameters.
c
A reference to a character.
s
A pointer to an array of characters.
n
The maximum number of characters to store, including the ternimating null character.
This is an integer value of type streamsize.
delim
The delimiter. The operation of extracting succesive characters is stopped when
the delimiter is read. This parameter is optional, if not specified the function
considers '\n' the delimiter.
sb
Output stream buffer: An object of class streambuf or any of its derived classes.
Return Value.
If above is not specified otherwise, the function returns *this
Example.
// istream get
#include <iostream>
#include <fstream>
using namespace std;
int main () {
char c, str[256];
ifstream is;
cout << "Enter the name of an existing text file: ";
cin.get (str,256);
is.open (str); // open file
while (is.good()) // loop while extraction from file is possible
{
c = is.get(); // get character from file
cout << c;
}
is.close(); // close file
return 0;
}
This example prompts for the name of an existing text file and prints its
content on screen.
Basic template member declarations ( basic_istream<charT,traits> ):
typedef charT char_type;
int_type get();
basic_istream& get (char_type& c );
basic_istream& get (char_type* s, streamsize n );
basic_istream& get (char_type* s, streamsize n, char_type delim );
basic_istream& get (basic_streambuf<char_type,traits>& sb);
basic_istream& get (basic_streambuf<char_type,traits>& sb, char_type
delim );
////////////////////////////////////////////////////////////////////
fstream class provides a stream interface to read and
write data from/to files.
The class mantains internally (privately) a pointer to a filebuf object in charge
of the interaction with the file. This pointer can be obtained/modified by calling
member rdbuf.
The file to be processed can be specified as a parameter for the constructor
or by calling member open.
After a file is processed it can be closed by calling member close. In this
case the file stream may be used to open another file.
Member is_open can be used to determine wether the stream is currently operating
on a file or not.
Please refer to parent classes for more details on information mantained by
an fstream object.
//////////////////////////////////////////////////////////////////////////
ifstream class provides a stream interface to read data
from files.
The class mantains internally (privately) a pointer to a filebuf object in charge
of the interaction with the file. This pointer can be obtained/modified by calling
member rdbuf.
The file to be processed can be specified as a parameter for the constructor
or by calling member open.
After a file is processed it can be closed by calling member close. In this
case the file stream may be used to open another file.
Member is_open can be used to determine wether the stream is currently operating
on a file or not.
///////////////////////////////////////////////////////////////////////
ifstream ( );
explicit ifstream ( const char * filename, openmode mode = in );
Construct an object and optionally open a file.
Constructs an object of class ifstream. This implies the initialization of the
associated filebuf object and the call to its base class' constructor with the
filebuf object as constructor parameter.
Additionally, in case the second constructor syntax is used, the stream's file
is associated with a physical file as if a call to member open with the same
parameters was made.
Parameters.
filename
String containing the name of the file to be opened.
mode
Flags describing the requested i/o mode for the file. This is an object of type
ios_base::openmode, it generally consist of a combination of the following flags
(member constants):
bit effect
app (append) Seek to the end of the stream before each output operation.
ate (at end) Seek to the end of the stream when opening.
binary Consider stream as binary rather than text.
in Allow input operations on a stream.
out Allow output operations on a stream.
trunc (truncate) Truncate file to zero when opening.
Return Value.
none
Example.
// using ifstream constructors.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream infile ("test.txt");
while (infile.good())
cout << (char) infile.get();
infile.close();
return 0;
}
This example opens a file and prints its content to the standard output device.
Basic template member declaration ( basic_ifstream<charT,traits> ):
basic_ifstream();
explicit basic_ifstream ( const char * filename, openmode mode = in );
///////////////////////////////////////////////////////////
char * strtok ( const char * string, const char * delimiters
);
Sequentially truncate string if delimiter is found.
If string is not NULL, the function scans string for the first occurrence of
any character included in delimiters. If it is found, the function overwrites
the delimiter in string by a null-character and returns a pointer to the token,
i.e. the part of the scanned string previous to the delimiter.
After a first call to strtok, the function may be called with NULL as string
parameter, and it will follow by where the last call to strtok found a delimiter.
delimiters may vary from a call to another.
NOTE: if no delimeter is found, it will return everything up to the null character.
Parameters.
string
Null-terminated string to scan.
separator
Null-terminated string containing the separators.
Return Value.
A pointer to the last token found in string. NULL is returned when there are
no more tokens to be found.
Portability.
Defined in ANSI-C.
Example.
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="This is a sample string,just testing.";
char * pch;
printf ("Splitting string \"%s\" in tokens:\n",str);
pch = strtok (str," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.");
}
return 0;
}
Output:
Splitting string "This is a sample string,just testing." in tokens:
This
is
a
sample
string
just
testing
//////////////////////////////////////////////////////////////////