vc++ mfc-i can't make getline work with a string for file input

Reply

Join Date: Jun 2005
Posts: 4
Reputation: nasaiya is an unknown quantity at this point 
Solved Threads: 0
nasaiya nasaiya is offline Offline
Newbie Poster

vc++ mfc-i can't make getline work with a string for file input

 
0
  #1
Jun 5th, 2005
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:


  1. #include <fstream>
  2. #include <string>
  3. using std::string;
  4. using std::getline; /*i need to do this for getline to be recognized...could
  5. std be the wrong one??*/
  6.  
  7. ifstream ccin;
  8.  
  9. ...
  10.  
  11. ccin.open(data.dat);
  12. string str;
  13. getline(ccin,str);//the problem :(
  14. ccin.close();
  15.  
  16. ...

am i retarded? or is the compiler...
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: vc++ mfc-i can't make getline work with a string for file input

 
0
  #2
Jun 6th, 2005
>> ccin.open(data.dat);
I think you mean
  1. 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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 4
Reputation: nasaiya is an unknown quantity at this point 
Solved Threads: 0
nasaiya nasaiya is offline Offline
Newbie Poster

Re: vc++ mfc-i can't make getline work with a string for file input

 
0
  #3
Jun 6th, 2005
Originally Posted by Dogtree
>> ccin.open(data.dat);
I think you mean
  1. 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)
  1. // Cust.cpp : Defines the class behaviors for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Cust.h"//this is App's main class declaration file
  6. #include "CustDlg.h"//this is the app's main dialog
  7.  
  8. #include <fstream.h>
  9. #include <string>
  10. #include <cstring>
  11.  
  12. using std::string;i need this or else string is undeclared identifier
  13. using std::endl;
  14. using std::getline;// i need this or else getline is an undeclared identifier
  15.  
  16.  
  17.  
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26.  
  27. // CCustApp
  28.  
  29. BEGIN_MESSAGE_MAP(CCustApp, CWinApp)
  30. //{{AFX_MSG_MAP(CCustApp)
  31. // NOTE - the ClassWizard will add and remove mapping macros here.
  32. // DO NOT EDIT what you see in these blocks of generated code!
  33. //}}AFX_MSG
  34. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  35. END_MESSAGE_MAP()
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38.  
  39.  
  40. // CCustApp construction
  41. //
  42. //
  43. // ********THIS IS WHERE I'm TRYING TO DO THE FILE INPUT********
  44. //////////////////////////////////////////////////////////////////////////////
  45. CCustApp::CCustApp()
  46. {
  47. ifstream ccin;
  48. for (int a=0;a<10;a++) //set default valuse for all custs struct variables (all are CStrings)
  49. {
  50. custs[a].pname="emt";
  51. custs[a].pname="emt";
  52. custs[a].padd="emt";
  53. custs[a].padd2="emt";
  54. custs[a].pcity="emt";
  55. custs[a].pstate="emt";
  56. custs[a].pzip="emt";
  57. custs[a].pcountry="emt";
  58. custs[a].pphone="emt";
  59. custs[a].pemail="emt";
  60. custs[a].bname="emt";
  61. custs[a].badd="emt";
  62. custs[a].badd2="emt";
  63. custs[a].bcity="emt";
  64. custs[a].bstate="emt";
  65. custs[a].bzip="emt";
  66. custs[a].bcountry="emt";
  67. custs[a].bphone="emt";
  68. custs[a].bemail="emt";
  69. custs[a].sname="emt";
  70. custs[a].sadd="emt";
  71. custs[a].sadd2="emt";
  72. custs[a].scity="emt";
  73. custs[a].sstate="emt";
  74. custs[a].szip="emt";
  75. custs[a].scountry="emt";
  76. custs[a].sphone="emt";
  77. }
  78.  
  79. ccin.open("data.dat");
  80.  
  81. // THIS IS WHERE I'VE BEEN TRYING A MILLION DIFFERENT THINGS FOR INPUT
  82.  
  83. string pname; //std::string
  84.  
  85. getline(ccin,pname);
  86.  
  87. ccin.close();
  88.  
  89. }
  90.  
  91. CCustApp::~CCustApp()
  92. {
  93. WFile();
  94. }
  95. /////////////////////////////////////////////////////////////////////////////
  96. // The one and only CCustApp object
  97.  
  98.  
  99. /////////////////////////////////////////////////////////////////////////////
  100. // CCustApp initialization
  101.  
  102. BOOL CCustApp::InitInstance()
  103. {
  104. AfxEnableControlContainer();
  105.  
  106. // Standard initialization
  107. // If you are not using these features and wish to reduce the size
  108. // of your final executable, you should remove from the following
  109. // the specific initialization routines you do not need.
  110.  
  111. #ifdef _AFXDLL
  112. Enable3dControls(); // Call this when using MFC in a shared DLL
  113. #else
  114. Enable3dControlsStatic(); // Call this when linking to MFC statically
  115. #endif
  116.  
  117. CCustDlg dlg;
  118. m_pMainWnd = &dlg;
  119. int nResponse = dlg.DoModal();
  120. if (nResponse == IDOK)
  121. {
  122. // TODO: Place code here to handle when the dialog is
  123. // dismissed with OK
  124. }
  125. else if (nResponse == IDCANCEL)
  126. {
  127. // TODO: Place code here to handle when the dialog is
  128. // dismissed with Cancel
  129. }
  130.  
  131. // Since the dialog has been closed, return FALSE so that we exit the
  132. // application, rather than start the application's message pump.
  133. return FALSE;
  134. }
  135. void CCustApp::WFile()
  136. {
  137. ofstream ccout;
  138.  
  139. // this part is jsut a big for loop with a bunch of ccouts (not important)
  140. }

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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: vc++ mfc-i can't make getline work with a string for file input

 
0
  #4
Jun 6th, 2005
>> #include <fstream.h>
Mixing old and new headers will bite you every time.
  1. #include <fstream>
  2.  
  3. 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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 4
Reputation: nasaiya is an unknown quantity at this point 
Solved Threads: 0
nasaiya nasaiya is offline Offline
Newbie Poster

Re: vc++ mfc-i can't make getline work with a string for file input

 
0
  #5
Jun 6th, 2005
Originally Posted by Dogtree
>> #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>?
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: vc++ mfc-i can't make getline work with a string for file input

 
0
  #6
Jun 6th, 2005
>> 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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 4
Reputation: nasaiya is an unknown quantity at this point 
Solved Threads: 0
nasaiya nasaiya is offline Offline
Newbie Poster

Re: vc++ mfc-i can't make getline work with a string for file input

 
0
  #7
Jun 6th, 2005
okay thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC