Title of text file as an input of function

double function(){
string filename;
ifstream fileIN;
fileIN.open("Layout.txt");
}

I have a function that reads a txt file and saves the data into an array. What I want to do now is I want to put the "Layout.txt" as an input of function. It should look something like

double function(string Layout.txt){
string filename;
ifstream fileIN;
fileIN.open(Layout.txt);
}

obviously it doesnt work.. Please help!

Answers


Variable names can't contain .. They can contain letters, digits, and underscore (_), and they can't start with a digit. So what you can do is

void function(string filename) {
    ifstream fileIN;
    fileIN.open(filename);
}

I think you are confusing variable names, string values and filenames.

What you apparently need is a function like this:

double function(string const& filename){
    ifstream fileIN;
    fileIN.open(filename);
    // ...
}

Or in pre-C++11:

double function(string const& filename){
    ifstream fileIN;
    fileIN.open(filename.c_str());
    // ...
}

Then you call call it like this:

function("Layout.txt");

It does not make sense to hard-code a filename into the variable name of an apparently general-purpose function. You apparently want to use function with many different files, so why use a variable name that makes it appear as if it only worked with one particular file?

Chances are that your function really only needs to work with the "Layout.txt" file. In that case, you don't need a parameter at all:

double function(){
    ifstream fileIN;
    fileIN.open("Layout.txt");
    // ...
}

Need Your Help

Change values in rows

sql

I have the table Table1 which looks like this:

Resolving a shift/reduce conflict in an LALR parser

parsing yacc lalr ply

I've been using PLY to build up a parser for my language, however I've got a shift/reduce conflict that's causing me some trouble. My language has generic types with a syntax ala C++ templates. So