Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 838
  • Last Modified:

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()
0
asmara
Asked:
asmara
  • 6
1 Solution
 
nietodCommented:
Just a stab, try

     fh.getline(&a[0],15,"|");
     fh.getline(&p[0],15,"\n");
0
 
nietodCommented:
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.
0
 
asmaraAuthor Commented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nietodCommented:
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?
0
 
nietodCommented:
Nope.  Now I see it. Try.

     fh.getline(a,15,'|');
          fh.getline(p,15,'\n');
0
 
nietodCommented:
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.  
0
 
nietodCommented:
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now