Hi there!

I'm new to c++ and I'm having problems with ifstream. I assume that my question is pretty stupid but here you go:

I've got something like:

ifstream infile(inname);

Furthermore I do have an array - let's call it Array - which contains several filenames I want to choose one from. With wrong synthax I want:

ifstream infile(Array[5]);

But as I figured - that doesn't work as ifstream seems to expect char as input.
Can you show me the direction here?


>let's call it Array
Let's not. How about you write a small program that fails, and one of us can tell you why.

>Can you show me the direction here?
No. Taken in isolation, your code is correct. Post something with more substance if you want help.

if Array is of type string, then the correct declaration would be :

ifstream readFile(Array[5].c_str()];

Well sure - here you go:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

string array[5];
	short loop=0;	
	string line; 
	ifstream filelist ("filelist.txt"); 	
	while (! filelist.eof() )
	getline (filelist,line); 
	array[loop] = line;
	cout << array[loop] << endl; 

int imax=4; 
int imaxX=2; 
int imaxY=600;
int ioutX=2; 
int ioutY=6; 

cout << array[1] << endl; // <- WORKS FINE HERE

   double Matrix[imaxX+imaxY][imaxX+imaxY]; 

    int i, j;

    ifstream infile(array[1]); // <-- PROBLEM HERE

    for (i = 0; i < imaxY; i++) {
        for (j = 0; j < imaxX; j++) {
            infile >> Matrix[i][j];
    return 0;

It crashes at the ifstream line.

Thank you in advance!

In thanks to firstPerson - consider this problem as solved :)

I found this same problem. The ifstream wants a 'const char,' but I wanted to feed it a value through a variable. I ended up setting up a number of if statements to open the proper file. I considered it a 'stupid' fix (as in I was too stupid to do it a better way), but I don't know if there is a better way. Does someone else know?