Solved

G++ and fstream

Posted on 1998-04-20
7
812 Views
Last Modified: 2013-12-14
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
Comment
Question by:asmara
  • 6
7 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1162390
Just a stab, try

     fh.getline(&a[0],15,"|");
     fh.getline(&p[0],15,"\n");
0
 
LVL 22

Expert Comment

by:nietod
ID: 1162391
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
 

Author Comment

by:asmara
ID: 1162392
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 22

Expert Comment

by:nietod
ID: 1162393
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
 
LVL 22

Accepted Solution

by:
nietod earned 30 total points
ID: 1162394
Nope.  Now I see it. Try.

     fh.getline(a,15,'|');
          fh.getline(p,15,'\n');
0
 
LVL 22

Expert Comment

by:nietod
ID: 1162395
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
 
LVL 22

Expert Comment

by:nietod
ID: 1162396
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now