Hi can any one expalin me this piece of code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srikar
    New Member
    • Sep 2006
    • 62

    Hi can any one expalin me this piece of code

    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();
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by srikar
    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();
    If you post the code using code tags, it will be easier to read it

    Comment

    • srikar
      New Member
      • Sep 2006
      • 62

      #3
      Hi when I am using this code in my program I am getting the following errors

      these are the errors
      doc.cxx:35: no type named `_Ios_Fmtflags' in `class std::ios_base'
      doc.cxx: In function `vendor_map_ttb string_t* init_vendor_tab le(char*)':
      doc.cxx:98: no matching function for call to `std::strstream ::setf(const
      std::_Ios_Openm ode&)'
      /usr/include/c++/3.2.3/bits/ios_base.h:345: candidates are: std::_Ios_Fmtfl ags
      std::ios_base:: setf(std::_Ios_ Fmtflags)
      /usr/include/c++/3.2.3/bits/ios_base.h:353: std::_Ios_Fmtfl ags
      std::ios_base:: setf(std::_Ios_ Fmtflags, std::_Ios_Fmtfl ags)
      doc.cxx:102: no matching function for call to `std::basic_ifs tream<char,
      std::char_trait s<char> >::getline(std: :strstreambuf&, char)'
      /usr/include/c++/3.2.3/bits/istream.tcc:664 : candidates are:
      std::basic_istr eam<_CharT, _Traits>& std::basic_istr eam<_CharT,
      _Traits>::getli ne(_CharT*, long int, _CharT) [with _CharT = char, _Traits =
      std::char_trait s<char>]
      /usr/include/c++/3.2.3/istream:176: std::basic_istr eam<_CharT,
      _Traits>& std::basic_istr eam<_CharT, _Traits>::getli ne(_CharT*, long int)
      [with _CharT = char, _Traits = std::char_trait s<char>]

      this is the code

      void get_name_token ( strstream &line, ostrstream & output )
      { //chaitan s->line
      // Set line stream for input operations.

      //_Ios_Fmtflags f;
      ios_base::_Ios_ Fmtflags::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 enĂ d 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.

      // _Ios_Fmtflags f; //chaitan added declaration

      strstream line;
      line.setf(ios:: out); //chaitan ios->ios_base ios::out

      // Get a line from the vendor file stream.

      vendor_instream .getline( *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();


      suggest me the modifications that should be made in this code

      Thank you in advance

      regards

      srikar

      Comment

      Working...