invalid conversion: istream to char*

Hey, I'm trying to read an istream into a file, but I'm getting the error that I invalidly tried converting std::istream to const char* . Here's the part of main() that mentions it:

1
2
3
4
5
6
7
8
9
10
11
12
//int main()
//...
   case 2: {
      ifstream ifs(argv[1]);
      if (!ifs) {
         cerr << progname << ": couldn't open " << argv[1] << endl;
         return 1;
      }
      eventList.fill(ifs);
      break;
   }
//... 


And here's the function outside of main() that mentions it:

1
2
3
4
5
6
7
8
9
10
11
12
void EventList::fill(istream& is)
{
    std::string line;
    ifstream myFile(is);
    if (myFile.is_open())
        {
            while (getline(myFile,line))
                eventListPQ.push(atoi(line));
            myFile.close();
        }
    else cout << "Cannot open file.\n";
}


I understand that I have an istream in fill() and a char* in main(), I just wanna know how I could make it work. And I can't change anything in main() by the way, it's pre-written by my professor.
Last edited on
I'm not sure exactly what you are trying to do. The ifstream constructor expects a string as argument but you are passing a stream object.

If you want the fill function to read from the same file as is why not simply use is inside fill without creating myFile?

If is contains a filename of a file that you want myFile to read from you will have to extract the filename into a string first, possibly by using getline.
The problem with replacing myFile with is, is that it's an istream, so it doesn't have the .is_open() or .close() functions.

I'm trying to take the data from an input file, convert it to integer and push it into a queue (entryListPQ)
Shouldn't you be testing if the file was opened before calling fill? Why call fill if it failed to open?
Isn't that what the code right before fill() does?
Yeah but you call fill no matter if the file could be opened or not.

You can check is the same way you check ifs.

Just some thoughts... The fill function receives a istream object so should it really assume it's a file? I mean if fill expects to read from a file why not make is a ifstream&?
No, fill() won't always read from a file. Here's the whole code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <queue>

using namespace std;

struct Event {
    enum EventKind {arrival, departure};
    EventKind type;
    int time;
    int length;
};

typedef priority_queue<Event> EventPQType;
typedef queue<Event> BankQType;

struct EventList {
    EventPQType eventListPQ;
    bool tellerAvailable;
    void fill(istream& is);
    void simulate();
};

bool operator< (const Event &e0, const Event &e1);
istream & operator>> (istream &is, Event &e);
void processArrival(const Event &e, EventPQType &epq, BankQType &bq, bool &tellerAvailable);
int processDeparture(const Event &e, EventPQType &epq, BankQType &bq, bool &tellerAvailable);

int main(int argc, char** argv)
{
   EventList eventList;

   // command-line parameter munging
   // also fills the event list with the input data
   char* progname = argv[0];            // simplify msgs a bit
   switch (argc) {
   case 1:
      eventList.fill(cin);
      break;
   case 2: {
      ifstream ifs(argv[1]);
      if (!ifs) {
         cerr << progname << ": couldn't open " << argv[1] << endl;
         return 1;
      }
      eventList.fill(ifs);
      break;
   }
   default:
      cerr << "Usage: " << progname << " [datafile]\n";
      return 2;
   }

   eventList.simulate();
}

void EventList::fill(istream& is) //where i'm stuck right now
{
    std::string line;
    ifstream myFile();
    if (myFile.is_open())
        {
            while (getline(myFile,line))
                eventListPQ.push(atoi(line));
            myFile.close();
        }
    else cout << "Cannot open file.\n";
}
ah my mistake.

In my opinion if the fill function accepts a istream& it should not assume it's a file stream. It could just as well be std::cin or some other input stream object. So in other words it shouldn't assume the stream can be opened or closed. I think it's the responsibility of whoever calls the fill function (main() in this case) to make sure the file is opened and closed correctly. Calling close() is not totally necessary because the file will be closed automatically in the ifstream destructor.
Topic archived. No new replies allowed.