Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Beginers Question about Getline (http://www.velocityreviews.com/forums/t285180-beginers-question-about-getline.html)

Skywise 08-24-2004 03:04 AM

Beginers Question about Getline
 
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.

zalzon 08-24-2004 03:57 AM

Re: Beginers Question about Getline
 
On 23 Aug 2004 20:04:36 -0700, theskywise@houston.rr.com (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.

Karl Heinz Buchegger 08-24-2004 07:31 AM

Re: Beginers Question about Getline
 
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
kbuchegg@gascad.at

Karl Heinz Buchegger 08-24-2004 07:32 AM

Re: Beginers Question about Getline
 
zalzon wrote:
>
> On 23 Aug 2004 20:04:36 -0700, theskywise@houston.rr.com (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
kbuchegg@gascad.at

Skywise 08-24-2004 10:37 AM

Re: Beginers Question about Getline
 
> 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.

Skywise 08-24-2004 01:51 PM

Re: Beginers Question about Getline
 
> 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)

Karl Heinz Buchegger 08-24-2004 02:54 PM

Re: Beginers Question about Getline
 
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
kbuchegg@gascad.at

Skywise 08-24-2004 08:35 PM

Re: Beginers Question about Getline
 
> 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.

Skywise 08-24-2004 08:48 PM

Re: Beginers Question about Getline
 
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... :(

Jon Bell 08-24-2004 09:44 PM

Re: Beginers Question about Getline
 
In article <2124737f.0408241235.5c1960ed@posting.google.com >,
Skywise <theskywise@houston.rr.com> 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 <jtbellm4h@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA


All times are GMT. The time now is 11:26 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.