Trying to Learn C++, Problems with iostream::getline() from stringheader - c++

This is a discussion on Trying to Learn C++, Problems with iostream::getline() from stringheader - c++ ; The following is not a complete program. Its supposed to read 2 lines from a config file and place what it reads into the line array. I am having problems using getline(). From what I have read, it looks like ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11

Trying to Learn C++, Problems with iostream::getline() from stringheader

  1. Default Trying to Learn C++, Problems with iostream::getline() from stringheader

    The following is not a complete program.
    Its supposed to read 2 lines from a config
    file and place what it reads into the line array.
    I am having problems using getline(). From what I have read,
    it looks like including the <string> header
    should allow getline() to put the data into my string.

    The errors I am getting are:
    |25|error: invalid conversion from ‘void*’ to ‘char**’|
    |25|error: cannot convert ‘std::string’ to ‘size_t*’ for
    argument ‘2’ to ‘__ssize_t getline(char**, size_t*, FILE*)’|

    It looks like the compiler is still
    trying to use getline() from the <iostream>
    header rather than the <string> header.

    Any suggestions?


    #include <iostream>
    #include <fstream.h>
    #include <assert.h>
    #include <string>

    using namespace std;

    int main()
    {
    // declare variables
    double avalue;
    double bvalue;
    double cvalue;
    string line[2][2];

    // Open configfile stream
    ifstream configfile;
    configfile.open("configtest.conf");
    assert(! configfile.fail());


    // Write data from file to line array

    for(int i = 0;i<2;i++){
    for(int j = 0;j<2;j++)
    getline(configfile,line[i][j]," = ");
    }

    cout<<line[0][0]<<endl;
    cout<<line[0][1]<<endl;
    cout<<line[1][0]<<endl;
    cout<<line[1][1]<<endl;


    // Close configfile stream - problem closing file
    configfile.close();
    assert(! configfile.fail());

    // Do math and display to user
    //cvalue = avalue + bvalue;
    //cout<< "A + B = "<< cvalue <<endl;
    return 0;
    }

  2. Default Re: Trying to Learn C++, Problems with iostream::getline() fromstring header

    On Feb 26, 6:51 pm, Jason Hodge <hodge.ja...@gmail.com> wrote:
    > <snip>
    >
    > #include <iostream>
    > #include <fstream.h>

    Should be <fstream>. The use of ".h" is pre-standard.

    > <snip code>
    > getline(configfile,line[i][j]," = ");


    The last parameter (i.e. the delimiter) that you need to pass is a
    char, and not a char *. So, the following should work:

    getline(configfile,line[i][j],'=');


  3. Default Re: Trying to Learn C++, Problems with iostream::getline() fromstring header

    On Tue, 26 Feb 2008 08:24:09 -0800, Abdullah wrote:


    > The last parameter (i.e. the delimiter) that you need to pass is a char,
    > and not a char *. So, the following should work:
    >
    > getline(configfile,line[i][j],'=');


    Thanks, that helped. Would you mind taking a second to help me understand
    the difference between "=" and '='? I understand the difference between a
    regular variable and a pointer (char vs char*), but I guess I don't
    understand the syntactic difference between "" and ''.

    Also, the reason I had " = " is because in my config file I had a line
    that looks like this:

    avalue = 1

    and I wanted to not include the white space on either side of the = when
    putting the data into my string array. Any suggestion on how to ignore or
    parse out the white space? My other option would be to simply change the
    config file lines to be:

    avalue=1 instead of avalue = 1

    Thanks!!!

  4. Default Re: Trying to Learn C++, Problems with iostream::getline() from string header

    Jason Hodge said:

    > On Tue, 26 Feb 2008 08:24:09 -0800, Abdullah wrote:
    >
    >
    >> The last parameter (i.e. the delimiter) that you need to pass is a char,
    >> and not a char *. So, the following should work:
    >>
    >> getline(configfile,line[i][j],'=');

    >
    > Thanks, that helped. Would you mind taking a second to help me understand
    > the difference between "=" and '='?


    C and C++ use "double-quotes" to delimit string literals, and
    'single-quotes' to delimit character literals. Thus:

    char foo[] = "foo";
    char bar = 'b';

    The difference is obvious (strings can have multiple characters in them,
    whereas characters with multiple characters in them are problematical in C
    and, I believe, illegal in C++), except in the situation where you have a
    string literal with a single character in it! That's when you need to be
    on your guard.

    "=" is a string literal, which comprises the '=' character and a null
    terminating character - two bytes altogether.

    '=' is a character literal, which in C++ has type char and is represented
    by a single byte. (Confusingly, in C it has type int and is represented by
    however many bytes there are in your implementation's int! But it's still
    only one character.)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

  5. Default Re: Trying to Learn C++, Problems with iostream::getline() fromstring header


    Thanks for the explanation!

  6. Default Re: Trying to Learn C++, Problems with iostream::getline() fromstring header

    On Feb 26, 9:49 pm, Jason Hodge <hodge.ja...@gmail.com> wrote:
    > On Tue, 26 Feb 2008 08:24:09 -0800, Abdullah wrote:
    > > The last parameter (i.e. the delimiter) that you need to pass is a char,
    > > and not a char *. So, the following should work:

    >
    > > getline(configfile,line[i][j],'=');

    >
    > Thanks, that helped. Would you mind taking a second to help me understand
    > the difference between "=" and '='? I understand the difference between a
    > regular variable and a pointer (char vs char*), but I guess I don't
    > understand the syntactic difference between "" and ''.
    >
    > Also, the reason I had " = " is because in my config file I had a line
    > that looks like this:
    >
    > avalue = 1
    >
    > and I wanted to not include the white space on either side of the = when
    > putting the data into my string array. Any suggestion on how to ignore or
    > parse out the white space?


    Inside the for loop, immediately after the getline, you can add:

    line[i][j].erase(s.length()-1); // erase data beginning at
    s.length()-1 onwards

    This should delete the last character. You can then use
    configfile.ignore() to skip the next whitespace.

    Alternatively, you could use ' ' as the delimiting character (if your
    file allows it), and then ignore 2 characters.

    Obviously, it will be more efficient to change the config file.

  7. Default Re: Trying to Learn C++, Problems with iostream::getline() from string header

    Jason Hodge wrote:

    > Also, the reason I had " = " is because in my config file I had a line
    > that looks like this:
    >
    > avalue = 1
    >
    > and I wanted to not include the white space on either side of the =
    > when putting the data into my string array. Any suggestion on how to
    > ignore or parse out the white space? My other option would be to
    > simply change the config file lines to be:
    >
    > avalue=1 instead of avalue = 1
    >
    > Thanks!!!


    You could pass your string through a function like this, to remove
    leading and trailing whitespace:

    std::string strip_ws(const std::string& orig)
    {
    size_t begin = orig.find_first_not_of(" \t");
    size_t end = orig.find_last_not_of(" \t");

    return orig.substr(begin, (end-begin)+1);
    }

    Bart v Ingen Schenau
    --
    a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
    c.l.c FAQ: http://c-faq.com/
    c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

  8. Default Re: Trying to Learn C++, Problems with iostream::getline() fromstring header

    Jason Hodge wrote:
    > On Tue, 26 Feb 2008 08:24:09 -0800, Abdullah wrote:
    >
    >
    >> The last parameter (i.e. the delimiter) that you need to pass is a char,
    >> and not a char *. So, the following should work:
    >>
    >> getline(configfile,line[i][j],'=');

    >
    > Thanks, that helped. Would you mind taking a second to help me understand
    > the difference between "=" and '='? I understand the difference between a
    > regular variable and a pointer (char vs char*), but I guess I don't
    > understand the syntactic difference between "" and ''.
    >
    > Also, the reason I had " = " is because in my config file I had a line
    > that looks like this:
    >
    > avalue = 1
    >
    > and I wanted to not include the white space on either side of the = when
    > putting the data into my string array. Any suggestion on how to ignore or
    > parse out the white space? My other option would be to simply change the
    > config file lines to be:
    >

    If you choose to stream rather than use getline(), you can set the
    stream's skipws flag.

    --
    Ian Collins.

  9. Default Re: Trying to Learn C++, Problems with iostream::getline() from string header

    Abdullah wrote:
    > On Feb 26, 6:51 pm, Jason Hodge <hodge.ja...@gmail.com> wrote:
    >> <snip>
    >>
    >> #include <iostream>
    >> #include <fstream.h>

    > Should be <fstream>. The use of ".h" is pre-standard.


    Even worse, the old IOStreams library from e.g. <fstream.h> and the standard
    one from <fstream> are distinct things which are not compatible! That and
    the use of 'using namespace std;' will absolutely wreak havoc on the
    symbols contained in the global namespace, and any further discussion of
    problems should be postponed to after fixing this.

    Uli


  10. Default Re: Trying to Learn C++, Problems with iostream::getline() from string header

    on Tue, 26 Feb 2008 10:19:59 -0800 (PST), Abdullah <plcoderREMOVETHIS@gmail.com>
    wrote this wisdom:

    >On Feb 26, 9:49 pm, Jason Hodge <hodge.ja...@gmail.com> wrote:
    >> On Tue, 26 Feb 2008 08:24:09 -0800, Abdullah wrote:
    >> > The last parameter (i.e. the delimiter) that you need to pass is a char,
    >> > and not a char *. So, the following should work:

    >>
    >> > getline(configfile,line[i][j],'=');

    >>
    >> Thanks, that helped. Would you mind taking a second to help me understand
    >> the difference between "=" and '='? I understand the difference between a
    >> regular variable and a pointer (char vs char*), but I guess I don't
    >> understand the syntactic difference between "" and ''.


    The difference is one is a string and the other is a char I.e
    "=" means two byte long string with = as the first char followed by terminating
    null byte so equates to
    '=', '\0'

    and

    '=' means a one byte char value of ... er ... '='



    --
    Simon.

    'Be Seeing You.
    Who is number one?
    I will not be pushed, filed, stamped, indexed, briefed, de-briefed or numbered.
    Registered Linux User #300464 Machine Id #188886
    Linux Counter - http://counter.li.org/
    Remove the s.p.a.m to reply

+ Reply to Thread
Page 1 of 2 1 2 LastLast