| | |
vc++ mfc-i can't make getline work with a string for file input
![]() |
•
•
Join Date: Jun 2005
Posts: 4
Reputation:
Solved Threads: 0
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:
am i retarded? or is the compiler...
heres a snippit just to illustrate:
C++ Syntax (Toggle Plain Text)
#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
>> 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?
I think you mean
C++ Syntax (Toggle Plain Text)
ccin.open("data.dat");

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?
•
•
Join Date: Jun 2005
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Dogtree
>> ccin.open(data.dat);
I think you mean
C++ Syntax (Toggle Plain Text)
ccin.open("data.dat");
(cust.cpp)
C++ Syntax (Toggle Plain Text)
// 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
Last edited by nasaiya; Jun 6th, 2005 at 4:49 pm. Reason: forgot the zip file lol
>> #include <fstream.h>
Mixing old and new headers will bite you every time.
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.
Mixing old and new headers will bite you every time.
C++ Syntax (Toggle Plain Text)
#include <fstream> using std::ifstream;
>> 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.
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.
![]() |
Similar Threads
- Search string in a text file (C)
- file input problems (with windows?) (Java)
- Reading file input into an array (C++)
Other Threads in the C++ Forum
- Previous Thread: Grrr C++ Help Me!
- Next Thread: Am I good enough for c++?
| Thread Tools | Search this Thread |
ace_thread api array based binary bitmap borland c++ c/c++ calling char class classes code coding compile console conversion count delete delete-line deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embedded encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream information input int integer java lib linkedlist linker loop looping loops map math mathhomeworkhelp matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion reference reverse richedit rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets





