using getline with ifstream

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manwanirg
    New Member
    • Feb 2007
    • 3

    using getline with ifstream

    the function getline is a public member of istream and cin.getline can be used.
    Since ifstream is publicily derived from istream, getline shall be available in ifstream as well. However,on solaris ver 2.7, getline is not recognised.

    e.g
    ifstream f("a.cpp");
    string s;
    f.getline(s,100 );

    why doesn't complier recognise fstream::getlin e(string,int) even though istream::getlin e(string,int) is recognized
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by manwanirg
    the function getline is a public member of istream and cin.getline can be used.
    Since ifstream is publicily derived from istream, getline shall be available in ifstream as well. However,on solaris ver 2.7, getline is not recognised.

    e.g
    ifstream f("a.cpp");
    string s;
    f.getline(s,100 );

    why doesn't complier recognise fstream::getlin e(string,int) even though istream::getlin e(string,int) is recognized
    your string s should be a char *, e.g.
    Code:
    ifstream f("a.cpp");
    char s[50];
    f.getline(s,100);

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      If you want to keep it as a string, I've always used

      Code:
      getline(istreamVar, stringVar);
      where istreamVar is either cin or my ifstream variable, and stringVar is the string you are trying to initialize.

      Comment

      Working...