i'm trying to use getline to read from a file into a std::string and when i give it the normal 2 parameters (my ifstream and the string) the compiler says it wants 3 arguments , but when i give it 3 it says it wants 2 arguments. this makes absolutly no sense to me! plus i can't imagine what third parameter getline could possibly need anyway.

heres a snippit just to illustrate:

#include <fstream>
#include <string>
using std::string;
using std::getline; /*i need to do this for getline to be recognized...could 
std be the wrong one??*/

ifstream ccin;

...

ccin.open(data.dat);
string str;
getline(ccin,str);//the problem :(
ccin.close();

...

am i retarded? or is the compiler...

>> ccin.open(data.dat);
I think you mean

ccin.open("data.dat");

>> getline(ccin,str);//the problem :(
Looks okay to me. What compiler are you using? Can you give us a short and complete program to compile that gives you the error? Can you post the error?

>> ccin.open(data.dat);
I think you mean

ccin.open("data.dat");

Yea i did mean to include the quotes i was tired when i wrote that but i don't think i could make a short version of the program because it is a dialog based program so thers like 7 or 8 different files with all the classes and such and i don't get the error if it is just a normal console program. if you want to look at it i'll post the whole source in a zip file. here is the file where the problem is :
(cust.cpp)

// Cust.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "Cust.h"//this is App's main class declaration file
#include "CustDlg.h"//this is the app's main dialog

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

using std::string;i need this or else string is undeclared identifier
using std::endl;
using std::getline;// i need this or else getline is an undeclared identifier




#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
	
// CCustApp

BEGIN_MESSAGE_MAP(CCustApp, CWinApp)
	//{{AFX_MSG_MAP(CCustApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////


// CCustApp construction
// 
//
// ********THIS IS WHERE I'm TRYING TO DO THE FILE INPUT********
//////////////////////////////////////////////////////////////////////////////
CCustApp::CCustApp()
{
	ifstream ccin;
	for (int a=0;a<10;a++) //set default valuse for all custs struct variables (all are CStrings)
	{
		custs[a].pname="emt";
		custs[a].pname="emt";
		custs[a].padd="emt";
		custs[a].padd2="emt";
		custs[a].pcity="emt";
		custs[a].pstate="emt";
		custs[a].pzip="emt";
		custs[a].pcountry="emt";
		custs[a].pphone="emt";
		custs[a].pemail="emt";
		custs[a].bname="emt";
		custs[a].badd="emt";
		custs[a].badd2="emt";
		custs[a].bcity="emt";
		custs[a].bstate="emt";
		custs[a].bzip="emt";
		custs[a].bcountry="emt";
		custs[a].bphone="emt";
		custs[a].bemail="emt";
		custs[a].sname="emt";
		custs[a].sadd="emt";
		custs[a].sadd2="emt";
		custs[a].scity="emt";
		custs[a].sstate="emt";
		custs[a].szip="emt";
		custs[a].scountry="emt";
		custs[a].sphone="emt";	
	}

	ccin.open("data.dat");

// THIS IS WHERE I'VE BEEN TRYING A MILLION DIFFERENT THINGS FOR INPUT

	string pname; //std::string

	getline(ccin,pname);
	
	ccin.close();
	
}

CCustApp::~CCustApp()
{
	WFile();
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CCustApp object


/////////////////////////////////////////////////////////////////////////////
// CCustApp initialization

BOOL CCustApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CCustDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}
void CCustApp::WFile()
{
       ofstream ccout;

//	this part is jsut a big for loop with a bunch of ccouts (not important)
}

THESE are the errors i get with the above code:

C:\Documents and Settings\Dave\Desktop\Cust\Cust.cpp(91) : 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'

C:\Documents and Settings\Dave\Desktop\Cust\Cust.cpp(91) : 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

c:\program files\microsoft visual studio\vc98\include\string(149) : see declaration of 'getline'
Error executing cl.exe.

I tried adding "30" as a third argument, but i just get the same errors only it says expected 2 arguments - 3 provided

NOTE: SORRY I COULDN'T UPLOAD THE FILE IT WAS A LITTLE TOO LARGE SO I'LL JUST MAKE A NEW PROGRAM AND REALLY QUICK AAND POST IT WHEN ITS DONE

the compiler i'm using is whatever one comes with ms visual c++ 6.0


thanks in advance

>> #include <fstream.h>
Mixing old and new headers will bite you every time.

#include <fstream>

using std::ifstream;

That's why it's a good idea to cut and paste the code you have from the start rather re-type it in the post.

>> #include <fstream.h>
Mixing old and new headers will bite you every time.

HOORAY!!! it works!!!

thank you sooo much!!

What do you mean old and new... is <fstream.h> different than <fstream>?

>> is <fstream.h> different than <fstream>?
Yes, very different. <fstream> is a standard modification of the prestandard <fstream.h>, where all of the classes are turned into template classes and a bunch of other relatively minor changes are made. Unless you're forced to write legacy code on a legacy system, always use the standard headers. You can tell them apart because the standard headers don't have a .h extension.

This article has been dead for over six months. Start a new discussion instead.