Solved

G++ and fstream

Posted on 1998-04-20
7
821 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
missing "main" in Java on Eclipse 4 65
convert char array to number in c 5 83
eclipse compiler vs Installed JREs option 3 75
c++, dynamic object by json 1 22
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

914 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

20 Experts available now in Live!

Get 1:1 Help Now