473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.
Jul 22 '05 #1
10 5582
On 23 Aug 2004 20:04:36 -0700, th********@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.
Jul 22 '05 #2
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
kb******@gascad.at
Jul 22 '05 #3
zalzon wrote:

On 23 Aug 2004 20:04:36 -0700, th********@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
kb******@gascad.at
Jul 22 '05 #4
> 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.
Jul 22 '05 #5
> 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)
Jul 22 '05 #6
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
kb******@gascad.at
Jul 22 '05 #7
> 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.
Jul 22 '05 #8
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... :(
Jul 22 '05 #9
In article <21**************************@posting.google.com >,
Skywise <th********@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 <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #10
jmh
Skywise wrote:
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

Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: seia0106 | last post by:
Hello I am writing a function to read a binary file. Here is a part of code #include <fstream> .. .. BYTE *pData; long lDataLen; pms->GetPointer(&pData); lDataLen = pms->GetSize(); // Read...
1
by: CG-P | last post by:
Does anyone know of a beginers guide website ? Chris
1
by: ma740988 | last post by:
Consider: ifstrem MyFile("extractMe.txt"); string Str; getline(MyFile, Str); getline above extracts the contents of MyFile and place into the string object. Deduced using FROM/TO logic I...
6
by: JustSomeGuy | last post by:
Getline has a delimiter character parameter that is used to say when input should stop. (std::cin.getline) However what do you do if you have multiple conditions/character on which you wish to...
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
2
by: Assertor | last post by:
Hi, All. (VC++6.0) I found some strange thins when using getline() and seekg() of std::ifstream. After the file position of an open file was shift to the end of the file, seekg() did not...
11
by: Key9 | last post by:
Hi all This is an simple app : read cin and print to cout I want it's action like "cat" with out arguement. #include <iostream> #include <string> using namespace std;
6
by: xiaobaiyang | last post by:
which text book is just for the c++ beginers? thks
6
by: bryant058 | last post by:
#include<iostream> #include<cstring> #include<string> #include<iomanip> using namespace std; int main() { string s; char *tokenptr; int space;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.