Beginers Question about Getline

Discussion in 'C++' started by Skywise, Aug 24, 2004.

  1. Skywise

    Skywise Guest

    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.
     
    Skywise, Aug 24, 2004
    #1
    1. Advertising

  2. Skywise

    zalzon Guest

    On 23 Aug 2004 20:04:36 -0700, (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.
     
    zalzon, Aug 24, 2004
    #2
    1. Advertising

  3. 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
     
    Karl Heinz Buchegger, Aug 24, 2004
    #3
  4. zalzon wrote:
    >
    > On 23 Aug 2004 20:04:36 -0700, (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
     
    Karl Heinz Buchegger, Aug 24, 2004
    #4
  5. Skywise

    Skywise Guest

    > 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, Aug 24, 2004
    #5
  6. Skywise

    Skywise Guest

    > 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)
     
    Skywise, Aug 24, 2004
    #6
  7. 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
     
    Karl Heinz Buchegger, Aug 24, 2004
    #7
  8. Skywise

    Skywise Guest

    > 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, Aug 24, 2004
    #8
  9. Skywise

    Skywise Guest

    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... :(
     
    Skywise, Aug 24, 2004
    #9
  10. Skywise

    Jon Bell Guest

    In article <>,
    Skywise <> 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 <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Jon Bell, Aug 24, 2004
    #10
  11. Skywise

    Skywise Guest

    (Jon Bell) wrote in message news:<cggcr7$130$>...
    > In article <>,
    > Skywise <> 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.


    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.
     
    Skywise, Aug 25, 2004
    #11
  12. Skywise

    jmh Guest

    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
     
    jmh, Aug 26, 2004
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy

    A Question about getline

    JustSomeGuy, Sep 26, 2004, in forum: C++
    Replies:
    6
    Views:
    2,712
    John Harrison
    Sep 27, 2004
  2. Replies:
    6
    Views:
    414
    blangela
    Mar 9, 2007
  3. Sapote

    strings for beginers

    Sapote, Mar 7, 2009, in forum: Python
    Replies:
    2
    Views:
    219
    Tim Wintle
    Mar 7, 2009
  4. Additya

    Html for Beginers

    Additya, Dec 8, 2009, in forum: HTML
    Replies:
    9
    Views:
    573
    Jukka K. Korpela
    Dec 10, 2009
  5. Replies:
    1
    Views:
    236
    Seebs
    Nov 9, 2010
Loading...

Share This Page