Solved

G++ and fstream

Posted on 1998-04-20
7
823 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
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…

803 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