dkim18
asked on
Passing input file name as parameter
I am trying to make funciton that read in file. The file name comes from main
How do I pass input file name as parameter?
Instead of reading file in main somethink like ifstream inFile("input.dat"),
I want to make function.
ReadInputFile(.......);
How do I pass input file name as parameter?
Instead of reading file in main somethink like ifstream inFile("input.dat"),
I want to make function.
ReadInputFile(.......);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I guess I mislead you. What I meant by was like this:
----------------------
void ReadInputFile(const char * generated_data_file)
{
ifstream inFile(generated_data_file );
}
int main()
{
…
char[] generated_data_file = "input.dat";
ReadInputFile(generated_da ta_file);
…
}
-------------------------- --
this gives me error like this:
AnalyzeOutput.cpp: In function `void ReadInputFile(basic_string <char,stri ng_char
_traits<char>,__default_al loc_templa te<false,0 > >)':
AnalyzeOutput.cpp:22: no matching function for call to `ifstream::ifstream (stri
ng &)'
/usr/local/lib/gcc-lib/spa rc-sun-sol aris2.8/2. 95.2/../.. /../../inc lude/g++-3 /fst
ream.h:61: candidates are: ifstream::ifstream()
/usr/local/lib/gcc-lib/spa rc-sun-sol aris2.8/2. 95.2/../.. /../../inc lude/g++-3 /fst
ream.h:62: ifstream::ifstream(int)
/usr/local/lib/gcc-lib/spa rc-sun-sol aris2.8/2. 95.2/../.. /../../inc lude/g++-3 /fst
ream.h:63: ifstream::ifstream(int, char *, int)
/usr/local/lib/gcc-lib/spa rc-sun-sol aris2.8/2. 95.2/../.. /../../inc lude/g++-3 /fst
ream.h:65: ifstream::ifstream(const char *, int = ios::in, int =
436)
/usr/local/lib/gcc-lib/spa rc-sun-sol aris2.8/2. 95.2/../.. /../../inc lude/g++-3 /fst
ream.h:68: ifstream::ifstream(const ifstream &)
AnalyzeOutput.cpp: In function `int main()':
AnalyzeOutput.cpp:77: parse error before `['
AnalyzeOutput.cpp:80: `generated_data_file' undeclared (first use this function)
AnalyzeOutput.cpp:80: (Each undeclared identifier is reported only once
AnalyzeOutput.cpp:80: for each function it appears in.)
----------------------
void ReadInputFile(const char * generated_data_file)
{
ifstream inFile(generated_data_file
}
int main()
{
…
char[] generated_data_file = "input.dat";
ReadInputFile(generated_da
…
}
--------------------------
this gives me error like this:
AnalyzeOutput.cpp: In function `void ReadInputFile(basic_string
_traits<char>,__default_al
AnalyzeOutput.cpp:22: no matching function for call to `ifstream::ifstream (stri
ng &)'
/usr/local/lib/gcc-lib/spa
ream.h:61: candidates are: ifstream::ifstream()
/usr/local/lib/gcc-lib/spa
ream.h:62: ifstream::ifstream(int)
/usr/local/lib/gcc-lib/spa
ream.h:63: ifstream::ifstream(int, char *, int)
/usr/local/lib/gcc-lib/spa
ream.h:65: ifstream::ifstream(const char *, int = ios::in, int =
436)
/usr/local/lib/gcc-lib/spa
ream.h:68: ifstream::ifstream(const ifstream &)
AnalyzeOutput.cpp: In function `int main()':
AnalyzeOutput.cpp:77: parse error before `['
AnalyzeOutput.cpp:80: `generated_data_file' undeclared (first use this function)
AnalyzeOutput.cpp:80: (Each undeclared identifier is reported only once
AnalyzeOutput.cpp:80: for each function it appears in.)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you pointing out my mistake.
This is possible using the optional argc and argv parameters which will get filled with the command line arguments.
For example, if you declare you main function as:
int main( int argc, char *argv[] )
Then argc will contain the number of command line arguments (in addition to the program name, so argc should be >= 1) and argv is an array of strings with argc elements that contains the arguments.
For example, if you executable was called "a.out" and you called it as such:
> a.out blahblah output.dat "foobar roobar"
Then argc would be "4" (3 arguments + command name) and argv would be as follows:
argv[0] = "a.out"
argv[1] = "blahblah"
argv[2] = "output.dat"
argv[3] = "foobar roobar"
(note that the quotes do not appear in the string, i've included them here only to delimit the string)