Trying to use basic_string with the getine() function?

I'm trying to use the basic_string to read the data that getline returns.  I can do it ok when I use a character array like the sample below, but I would like to bypass it and just use the basic_strings.  Any ideas?

 ofstream out;
 ifstream in;
 string sBuff;


 in.open( "c:\\temp\\Long Names Btnbad.asc", ios::in, filebuf::sh_read );

 out.open( "c:\\temp\\btnbad.txt", ios::out, filebuf::sh_write );

 char sT[256];
 int count = 0;

 while( in.eof() == false )
 {

  // Code that reads in the line of records
     in.getline( sT, 256 );
 
   // would like to use something like this
  // Just cant figure out the syntax or the proper way for getline
 // and basic_string to work together
   //  in.getline(sBuff, 256);  

  sBuff = sT;

  sBuff += sBuff + "\n";


  cout << sBuff.c_str() << endl;

  // Writes to output file
  out.write( sBuff.c_str(), lstrlen( sBuff.c_str() ));

 }

 out.close();
 in.close();
eric07Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
nietodConnect With a Mentor Commented:
You need to use the global getline() function, not the member getline() function, like

  getline(in,sBuff);

Let me know if you have any questions
0
 
eric07Author Commented:
It gives me an error of:
 error C2065: 'getline' : undeclared identifier


Here is the changes I've made:

#include <string>
#include <fstream.h>
#include <iostream>
#include <windows.h>

using std::string;

void main()
{


      ofstream out;
      ifstream in;
      string sBuff;


      in.open( "c:\\temp\\Long Names Btnbad.asc", ios::in, filebuf::sh_read );

      out.open( "c:\\temp\\btnbad.txt", ios::out, filebuf::sh_write );

      char sT[256];
      int count = 0;

      while( in.eof() == false )
      {

            // Code that reads in the line of records
  //               in.getline( sT, 256 );

            getline(in, sBuff, ' ' );
            
   //            in.getline(sBuff, 256);  

       //      sBuff = sT;

            sBuff += sBuff + "\n";


            cout << sBuff.c_str() << endl;

            // Writes to output file
            out.write( sBuff.c_str(), lstrlen( sBuff.c_str() ));

      }

      out.close();
      in.close();
      
}
0
 
nietodCommented:
It should be in the "std" namespace, so try "std::getline()"

Also you've specified a space as a delimiter,  That means it will read up until it encounters a space.  Is that what you want?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
eric07Author Commented:
I did the following and got 4 errors:

#include <string>
#include <fstream.h>
#include <windows.h>


using std::string;

void main()
{
      ofstream out;
      ifstream in;
      string sBuff;

      in.open( "c:\\temp\\Long Names Btnbad.asc", ios::in, filebuf::sh_read );

      out.open( "c:\\temp\\btnbad.txt", ios::out, filebuf::sh_write );

      int count = 0;

      while( in.eof() == false )
      {

            // Code that reads in the line of records
            std::getline(in, sBuff );

            sBuff += sBuff + "\n";


            cout << sBuff.c_str() << endl;

            // Writes to output file
            out.write( sBuff.c_str(), lstrlen( sBuff.c_str() ));

      }

      out.close();
      in.close();
      
}

The errors are:

D:\Sample_Dev\io\io.cpp(24) : error C2780: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : expects 3 arguments - 2 provided
        d:\applications\microsoft visual studio\vc98\include\string(149) : see declaration of 'getline'
D:\Sample_Dev\io\io.cpp(24) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'class std::basic_istream<_E,_Tr> &
' from 'class ifstream'
D:\Sample_Dev\io\io.cpp(24) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'class std::basic_istream<_E,_Tr> &
' from 'class ifstream'
D:\Sample_Dev\io\io.cpp(24) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'class std::basic_istream<_E,_Tr> &
' from 'class ifstream'
Error executing cl.exe.

io.exe - 4 error(s), 0 warning(s)

If I'm doing something wrong please excuse me as I'm just learning this.

Thanks
0
 
nietodCommented:
I hate templated and namespace!  I suspect they are the problem.  Just as a test try changing

using std::string;

to

using namespace std;

I suspect the problems will go away.  
0
 
nietodCommented:
Also change <fsream.h> to <fstream>
0
 
eric07Author Commented:
OK I re-did it like so:

#include <windows.h>
#include <string>
#include <fstream>
#include <iostream>

using namespace std;

void main()
{
      ofstream out;
      ifstream in;
      string sBuff;

      in.open( "c:\\temp\\Long Names Btnbad.asc", ios::in, filebuf::sh_read );

      out.open( "c:\\temp\\btnbad.txt", ios::out, filebuf::sh_write );

      while (std::getline(in, sBuff))
      {

            sBuff += sBuff + "\n";

            cout << sBuff.c_str() << endl;

            out.write( sBuff.c_str(), lstrlen( sBuff.c_str() ));

      }

      out.close();
      in.close();
      
}


Now there are atlease less errors:

--------------------Configuration: io - Win32 Debug--------------------
Compiling...
io.cpp
d:\sample_dev\io\io.cpp(14) : error C2039: 'sh_read' : is not a member of 'basic_filebuf<char,struct std::char_traits<char> >'
d:\sample_dev\io\io.cpp(14) : error C2065: 'sh_read' : undeclared identifier
d:\sample_dev\io\io.cpp(16) : error C2039: 'sh_write' : is not a member of 'basic_filebuf<char,struct std::char_traits<char> >'
d:\sample_dev\io\io.cpp(16) : error C2065: 'sh_write' : undeclared identifier
Error executing cl.exe.

io.exe - 4 error(s), 0 warning(s)

What is the difference when I use:
using namespace std;
and
using std::string:
using std::fstream;
etc.

Is it that the first assumes everything while the using std::string, etc. becomes more specific?

Thanks
0
 
nietodCommented:
Now I am really confused.  the fstream's open() member function only takes 2 parameters.  You are specifying a 3rd.  Why?  Why isn't that reported as an error?

>> Is it that the first assumes everything
>> while the using std::string,
>> etc. becomes more specific?
exactly.  the "using namespace" includes everythign that is in the names space.  I think that solves problems because the string<> template class and the getline template class both have lots of default template parameters that default to names define in the "std" namespace.

If that is the source of the problem, and it does look that way, then "using namespace std" is not the only way to solve the problem, only a very expedient one.  If you didn't want to include all of the std namespace, you coudl get around this by specifying some of the other template parameters, this might take some experimentation.
0
 
eric07Author Commented:
I was able to fix the problem, here is my code:

#include <string>
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
      std::ofstream out( "c:\\temp\\Btnbad.txt" );
      std::ifstream in( "c:\\temp\\Btnbad.asc");
      std::string sBuff;


      while (std::getline(in, sBuff))
       {
 
            std::cout << sBuff.c_str() << std::endl;

            sBuff += "\n";
            out.write( sBuff.data(), sBuff.size() );

        }

      return 0;
      
}

Your right about open only taking in 2 parameters, I use VC60 and when i type
in.open its showing that it needs 3 parameters. But I looked it up and it on the help file and it showed it correctly.  I guess what was confusing is that in streams there are so many methods that are the same.

Thanks for your help

Thanks
0
 
nietodCommented:
Now you can drop all the "std::"'s if you are goignto be "using namespace std"
0
All Courses

From novice to tech pro — start learning today.