infile using Variable filename?

So. I have a CSV Parser that I built. It works very well. The way it currently works is that I have the parser in a header file "CSV_Parser". So my .cpp code would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

#include <eigen3/Eigen/Dense>

#include "CSV_Parser.h"

using namespace std;

using Eigen;

int main()

{

CSV matrixA; //CSV is the parser class name

matrixA.extract(); //runs the parser

cout << matrixA.CSV_Matrix; //CSV_Matrix is the public name of a Eigen matrix which receives the stripped Matrix

}


This all works great. I am running Eclipse in Linux (Xubuntu to be precise). In the header file the .csv is opened as follows:

1
2
3
ifstream infile;

infile.open("/home/karrotman/Desktop/Matrix.csv");


What I would like is for the user to be able to change the file that the parser is opening through the main .cpp file. In other words, is there a way to create some variable, say "FileName" and do the following:

1
2
3
4
5
6
7
8
9
10
11
CSV firstMat;

string MatrixA = "/home/karrotman/Desktop/Matrix1.csv";

firstMat.extract(MatrixA);

CSV secondMat;

string MatrixB = "/home/karrotman/Desktop/Matrix2.csv";

secondMat.extract(MatrixB);


And then the .h would now say:

1
2
3
4
5
6
7
void extract(string FileName)

...

ifstream infile;

infile.open(FileName);


Obviously this does not work. Ultimately the goal is for me to open 4 .csvs at one time so I can do numerical operations on them.
Last edited on
IIRC ifstreams want an array of chars as they file, so use the c_str() method of the string class.
...EDIT my mistake it seems your idea worked!! Let me test it on multiple files.
Last edited on
Topic archived. No new replies allowed.