Hi can any one expalin me this piece of code
In this code line is used for taking the input stream as line input stream.
Please explain me Can I use getline() instead of line.
When I am using the above code I am fetting errors like
line undeclared.
Please help me to resolve my problem
void get_name_token ( strstream &line, ostrstream & output )
{
// Set line stream for input operations.
line.setf(ios:: in);
// "Stop" is the char that immediately follows an extracted token
// part in the input stream. If "Stop" is a tab, newline, or EOF
// character, it indicates the end of a token. I give "Stop" an
// arbitrary initial value that is definitely not a delimiter.
char Stop = 'a';
// Eat leading white space and check "Stop" for end of token.
// Quit loop when "Stop" signals the end of a token.
while ( line >> ws , (Stop != '\t' && Stop != '\n' && Stop != EOF ))
{
line.get ( *output.rdbuf() , ' ' ); // get next token part.
Stop = (char)line.peek (); // Check next char for end of token
if ( Stop == ' ' )
{
// Might be end of token or just an embedded space.
// get the "Stop" character to advance the streambuf ptr.
line.get ( Stop );
// If the next character is a space then its the end of the
// token, so we set Stop to EOF to break the while loop.
if ( line.peek() == ' ' )
{
Stop = EOF;
}
else
{
output << Stop;
}
}
}
output << ends;
}
vendor_map_tb_t init_vendor_tab le( char* vendor_file_nam e )
{
ifstream vendor_instream ( vendor_file_nam e );
if ( vendor_instream )
{
// Allocate the vendor_table;
vendor_map_tb_t vendor_table = new gtb(string_t, vendor_map_t)( &stringless, &stringequal s );
// eat initial leading blank lines and whitespaces, then
// check for EOF. Loop until the file is empty.
while ( vendor_instream >> ws, !vendor_instrea m.eof() )
{
// Declare "line" stream and set it for output operations.
strstream line;
line.setf(ios:: out);
// Get a line from the vendor file stream.
vendor_instream .get ( *line.rdbuf(), '\n' );
line << ends;
if ( line.peek() == EOF || line.peek() == '#' )
{
// The line is blank or a comment, so ignore it and start
// again with the next line.
line.clear ();
vendor_instream >> ws;
}
else
{
int count = 0;
ostrstream names[3];
// Get three tokens from the line.
while ( count < 3 ) get_name_token ( line, names[count++] );
// Ignore the rest of the line. This allows us to extend the
// the number of tokens on a line in some future release without
// breaking a translator that expects three tokens on the line.
line.clear();
In this code line is used for taking the input stream as line input stream.
Please explain me Can I use getline() instead of line.
When I am using the above code I am fetting errors like
line undeclared.
Please help me to resolve my problem
void get_name_token ( strstream &line, ostrstream & output )
{
// Set line stream for input operations.
line.setf(ios:: in);
// "Stop" is the char that immediately follows an extracted token
// part in the input stream. If "Stop" is a tab, newline, or EOF
// character, it indicates the end of a token. I give "Stop" an
// arbitrary initial value that is definitely not a delimiter.
char Stop = 'a';
// Eat leading white space and check "Stop" for end of token.
// Quit loop when "Stop" signals the end of a token.
while ( line >> ws , (Stop != '\t' && Stop != '\n' && Stop != EOF ))
{
line.get ( *output.rdbuf() , ' ' ); // get next token part.
Stop = (char)line.peek (); // Check next char for end of token
if ( Stop == ' ' )
{
// Might be end of token or just an embedded space.
// get the "Stop" character to advance the streambuf ptr.
line.get ( Stop );
// If the next character is a space then its the end of the
// token, so we set Stop to EOF to break the while loop.
if ( line.peek() == ' ' )
{
Stop = EOF;
}
else
{
output << Stop;
}
}
}
output << ends;
}
vendor_map_tb_t init_vendor_tab le( char* vendor_file_nam e )
{
ifstream vendor_instream ( vendor_file_nam e );
if ( vendor_instream )
{
// Allocate the vendor_table;
vendor_map_tb_t vendor_table = new gtb(string_t, vendor_map_t)( &stringless, &stringequal s );
// eat initial leading blank lines and whitespaces, then
// check for EOF. Loop until the file is empty.
while ( vendor_instream >> ws, !vendor_instrea m.eof() )
{
// Declare "line" stream and set it for output operations.
strstream line;
line.setf(ios:: out);
// Get a line from the vendor file stream.
vendor_instream .get ( *line.rdbuf(), '\n' );
line << ends;
if ( line.peek() == EOF || line.peek() == '#' )
{
// The line is blank or a comment, so ignore it and start
// again with the next line.
line.clear ();
vendor_instream >> ws;
}
else
{
int count = 0;
ostrstream names[3];
// Get three tokens from the line.
while ( count < 3 ) get_name_token ( line, names[count++] );
// Ignore the rest of the line. This allows us to extend the
// the number of tokens on a line in some future release without
// breaking a translator that expects three tokens on the line.
line.clear();
Comment