Link to home
Start Free TrialLog in
Avatar of asmara
asmara

asked on

G++ and fstream

The following is a code i had on AIX C Set++

------

#include <iostream.h>
#include <fstream.h>
 
void main(){
  //fstream fh;
  char a[16],b[16];
  ifstream fh;
  fh.open("text.txt",ios::in);
  if (!fh){
    cout << "This is bad " << endl;
  }else{
    while (!fh.eof()){
      fh.getline(a,15,"|");
      fh.getline(p,15,"\n");
      cout << a << "   " << b << endl;
    }
  }
}

I t works fine in C Set++ , but when I port it to GNU G++ it bombs with the following bobo

test.C:13: no matching function for call to `ifstream::get (char[16], int, char[2])'
/usr/local/lib/g++-include/iostream.h:164: candidates are: istream::get()
/usr/local/lib/g++-include/iostream.h:138:                 istream::get(streambuf &, char)
/usr/local/lib/g++-include/iostream.h:128:                 istream::get(signed char *, int, char)
/usr/local/lib/g++-include/iostream.h:126:                 istream::get(signed char &)
/usr/local/lib/g++-include/iostream.h:122:                 istream::get(unsigned char &)
/usr/local/lib/g++-include/iostream.h:121:                 istream::get(char &)
/usr/local/lib/g++-include/iostream.h:120:                 istream::get(unsigned char *, int, char)
/usr/local/lib/g++-include/iostream.h:118:                 istream::get(char *, int, char)
test.C:14: no matching function for call to `ifstream::get (ifstream, int, char[2])'
/usr/local/lib/g++-include/iostream.h:164: candidates are: istream::get()
Avatar of nietod
nietod

Just a stab, try

     fh.getline(&a[0],15,"|");
     fh.getline(&p[0],15,"\n");
Note you shouldn't have to do that, the compiler should treat a and p as char * pointers, but it sounds like it is not.  I guess you get what you pay for.
Avatar of asmara

ASKER

get is defined in istream class in iostream.h
and fstream class inherits from iostream.h
class ifstream : public fstreambase, public istream {
  public:
      :
      :

by conventation I should be able to get methods in istream, as I was able to in C Set++.
My question is what is wrong with G++ and by the way I am using gcc 2.8.1 and 2.7.2.1
I think you can use the get functions.  I beleive the problem is that the getline function is not converting the character array parameters (like char a[16]) to character pointers (like char *a).  Did you try the syntax change I proposed?
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The problem is that the function is defined in the G++ library to take a CHARACTER as the last argument.  In the library you were using before (as in the library I have) it is defined to take a STRING as the last argument.  
Note that the two functions it suggests that are close matches are

/usr/local/lib/g++-include/iostream.h:120:                 istream::get(unsigned char *, int, char)
/usr/local/lib/g++-include/iostream.h:118:                 istream::get(char *, int, char)

See the last parameter is "char" not "char *".  I suspect that "char *" is the standard and that the library you are using is little out of date or a little off the standard.