istream::getline()

This is a discussion on istream::getline() within the C++ Programming forums, part of the General Programming Boards category; I'm going by a fairly old book here (I'm not particulary bothered, it's a great book), so that might be ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    11

    istream::getline()

    I'm going by a fairly old book here (I'm not particulary bothered, it's a great book), so that might be my problem, but does istream::getline() still exist? My compiler gives me an error when I tell it:
    Code:
    const int bsz = 30;
    char* buf[bsz];
    ifstream("test.txt");
    initin.getline(buf, bsz);
    "Could not find match for istream::getline(char * *, const int) in function main(int, char * *)"

    Any help?
    EDIT I think this may be because all my calls to getline() are inside while(strstr(etc.)) which the compiler is also telling me are erronous. Exact code:
    Code:
    const int bsz = 30;
    char* buf[bsz];  //hold curr line
    while(!strstr(buf,"<style"))
      initin.getline(buf, bsz);
    initin being: ifstream initin(argv[1]);
    Giving: "Could not find a match for 'strstr(char * *, const int)' in function main(int, char * *)".
    Last edited by mousey; 10-18-2003 at 04:23 AM.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Doesn't getline take a char* argument, not a char**? If you want buf to be a simple C style string, declare it as:

    char buf[bsz];

    Notice I removed the *. If you do that getline should work. If you really did want an array of strings, then you have more work to do (like using new allocate memory for each string).

  3. #3
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Thats because you have created a pointer to an array of chars. You need a pointer which points to a char, ie. char *Buf;
    If you change your code to char buf[bsz], that should work.
    Be a leader and not a follower.

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    sorry jlou - at the time i wrote the reply no one had written a response.
    Be a leader and not a follower.

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    I use this function in the following manner:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
       ifstream file;
       file.open("myfile");
       string buffer;
       getline( file , buffer );
       cout << buffer << endl;
       return 0;
    }
    Compiled in Dev CPP. Just switching the parameters!
    Hope that helped!
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21