Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Beginers Question about Getline

Reply
Thread Tools

Beginers Question about Getline

 
 
Skywise
Guest
Posts: n/a
 
      08-24-2004
I keep getting the following error upon compiling:
c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664:
'class istream &__thiscall istream::getline(char *,int,char)' : cannot
convert parameter 1 from 'const char *' to 'char *'
Conversion loses qualifiers

I have a data file called Standard.udf

The Data File (not the problem): The data in the file is in text
format, and was created using a function in this program with fout.
Using notepad, I know the data file is not the problem. FYI: The
first line of data is "***************************************" and I
just want to skip it and go on to the next one.

Here is the problem function:

int UnitDef_Load(string sFileName)
{
int iERRORCODE = ERR_NONE;
iERRORCODE = UnitDefCleanList(false); // cleans linked list
if(iERRORCODE == ERR_NONE)
{
ifstream fin(sFileName.c_str());
if (!fin) // checks for file
{
return ERR_UNABLE_TO_OPEN; // error checking
}
else
{

// HERE IS THE CODE LOADING THE HEADER DATA!!!!
string Temp;
fin.getline(Temp,255); // PROBLEM LINE
};
fin.close();
}; // end if
return iERRORCODE;
};


I have tried everything I know to get this to work (I have been away
from c++ for awhile... just now getting back to it).
Could someone please explain how to make this work? (I have spent
hours online looking for examples, but have not found one that works).
Thank you.
 
Reply With Quote
 
 
 
 
zalzon
Guest
Posts: n/a
 
      08-24-2004
On 23 Aug 2004 20:04:36 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Skywise)
wrote:

> // HERE IS THE CODE LOADING THE HEADER DATA!!!!
> string Temp;
> fin.getline(Temp,255); // PROBLEM LINE
> };


What is that ; doing at the end of an else? Its not suppose to be
there.
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-24-2004
Skywise wrote:
>
>
> // HERE IS THE CODE LOADING THE HEADER DATA!!!!
> string Temp;
> fin.getline(Temp,255); // PROBLEM LINE


You cannot use a string variable with getline like that.
For using strings there is a standalone getline function, which
does what you want.

getline( fin, Temp );


(I know, I know. string's are not integrated with rest of the
standard classes as good as they should be).


--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-24-2004
zalzon wrote:
>
> On 23 Aug 2004 20:04:36 -0700, (E-Mail Removed) (Skywise)
> wrote:
>
> > // HERE IS THE CODE LOADING THE HEADER DATA!!!!
> > string Temp;
> > fin.getline(Temp,255); // PROBLEM LINE
> > };

>
> What is that ; doing at the end of an else? Its not suppose to be
> there.


But it's not an error.
The if-then-else is followed by an empty statement.
Totally legal.

--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
Skywise
Guest
Posts: n/a
 
      08-24-2004
> What is that ; doing at the end of an else? Its not suppose to be
> there.


Ok... I took it out. Still getting same error.
 
Reply With Quote
 
Skywise
Guest
Posts: n/a
 
      08-24-2004
> You cannot use a string variable with getline like that.
> For using strings there is a standalone getline function, which
> does what you want.
>
> getline( fin, Temp );
>


Ok, I did that. Then I got this error:

error C2065: 'getline' : undeclared identifier

I figured maybe I needed to write it like this:

std::getline( fin, Temp);

But that didn't work either, and I get these errors:

c:\c++ files\programs\stellardebug\unitcode.h(691) : 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'
c:\c++ files\programs\stellardebug\unitcode.h(691) : 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:\c++ files\programs\stellardebug\unitcode.h(691) : 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:\c++ files\programs\stellardebug\unitcode.h(691) : 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.

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

Please advise.

(By the way, I am grateful for your help. Thanks for trying)
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-24-2004
Skywise wrote:
>
> > You cannot use a string variable with getline like that.
> > For using strings there is a standalone getline function, which
> > does what you want.
> >
> > getline( fin, Temp );
> >

>
> Ok, I did that. Then I got this error:
>
> error C2065: 'getline' : undeclared identifier
>
> I figured maybe I needed to write it like this:
>
> std::getline( fin, Temp);
>
> But that didn't work either, and I get these errors:


First of all we need to confirm something.

The string class mentioned in your previous post, how did
you get it? Did you
#include <string>

I am worried because the previous error message tells us that
there is no conversion from const char* to char*. Which would
only be possible if there is a way for a string object to be
implicitely converted to a const char*, which there is none.

So: Are you using the std::string class?

The function getline is available when you
#include <string>

which again makes me think that you are not using std::string

Try this program. It should compile without a problem.


#include <string>
#include <iostream>
using namespace std;

int main()
{
string input;

getline( cin, input );
cout << "You entered " << input << endl;
}

--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
Skywise
Guest
Posts: n/a
 
      08-24-2004
> First of all we need to confirm something.
>
> The string class mentioned in your previous post, how did
> you get it? Did you
> #include <string>
>
> I am worried because the previous error message tells us that
> there is no conversion from const char* to char*. Which would
> only be possible if there is a way for a string object to be
> implicitely converted to a const char*, which there is none.
>
> So: Are you using the std::string class?
>
> The function getline is available when you
> #include <string>
>
> which again makes me think that you are not using std::string
>
> Try this program. It should compile without a problem.
>
>
> #include <string>
> #include <iostream>
> using namespace std;
>
> int main()
> {
> string input;
>
> getline( cin, input );
> cout << "You entered " << input << endl;
> }


At the begining of my program, I have:

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

using std::string;

then, I call the header containing the problem function.
I note that I had no trouble using fstream to write to a file using
this same header containing the problem function.

In addition, I created a brand new console program from scratch, and
cut and pasted the code you suggested. I got the following error:

c:\c++ files\programs\strings\strings.cpp(15) : fatal error C1010:
unexpected end of file while looking for precompiled header directive
Error executing cl.exe.

Strings.exe - 1 error(s), 0 warning(s)

Correct me if I am wrong, but are my header files in the wrong place
or something?

Please advise.
 
Reply With Quote
 
Skywise
Guest
Posts: n/a
 
      08-24-2004
I forgot to add that I am using VC++ 6.0, and I did add the DirectX
SDK stuff, etc... to it (of course, I am only pretty sure I followed
the directions correctly...
 
Reply With Quote
 
Jon Bell
Guest
Posts: n/a
 
      08-24-2004
In article <(E-Mail Removed) >,
Skywise <(E-Mail Removed)> wrote:
>
>#include <time.h>
>#include <fstream.h>
>#include <string>


<fstream.h> is not the standard header for file streams. You should use
<fstream> instead. In general, you should never mix the "old" (really,
ancient!) .h headers with the "new" (now six years old) standard non-.h
headers. They can be incompatible in sneaky ways. I'm not guaranteeing
that this is the cause of your problem, but you should fix it anyway.
Change <time.h> to <ctime> while you're at it.

--
Jon Bell <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best exercises for beginers to learn? brf256@gmail.com Python 1 11-09-2010 08:27 AM
Html for Beginers Additya HTML 9 12-10-2009 05:28 PM
strings for beginers Sapote Python 2 03-07-2009 09:29 PM
Studio Flash for Beginers Bruce Digital Photography 3 11-23-2008 11:47 PM
which text book is just for the c++ beginers? xiaobaiyang@gmail.com C++ 6 03-09-2007 05:34 PM



Advertisments