Beginers Question about Getline

S

Skywise

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.
 
Z

zalzon

// 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.
 
K

Karl Heinz Buchegger

Skywise said:
// 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).
 
K

Karl Heinz Buchegger

zalzon said:
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.
 
S

Skywise

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)
 
K

Karl Heinz Buchegger

Skywise said:
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;
}
 
S

Skywise

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.
 
S

Skywise

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... :(
 
J

Jon Bell

#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.
 
S

Skywise

<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.

That did it. I changed all my includes to the younger include files,
and got it to work with no errors. I want to express my personal
thanks to everyone who took the time to read my code and help me work
through the code and headers. I was begining to get frustrated.
Thank you again. You made my day.

Skywise.
 
J

jmh

Skywise said:
At the begining of my program, I have:

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

I don't think fstream.h or fstream provide everthing that
iostream provides. Try including iostream as well and see if
that doesn't solve your problem.

jmh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Question about my projects 3
getline problem 10
fstream, getline() and failbit 15
adapting getline 77
getline() special 1
problem using getline function 0
How to solve my bug with getline 0
getline problem 3

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top