Solved

Passing input file name as parameter

Posted on 2003-12-01
5
377 Views
Last Modified: 2008-02-07
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(.......);
0
Comment
Question by:dkim18
  • 3
  • 2
5 Comments
 
LVL 1

Assisted Solution

by:travd
travd earned 100 total points
ID: 9854427
Assuming that the file name is stored in an array of char, as it would be if it came from argv, then you can declare the function something like this:

void ReadInputFile( const char *filename )
{
   ...
}

You would call the function from main like this:

int main( int argc, char *argv[] )
{
    ...
    ReadInputFile( filename );
    ...
}


You might want to declare the ReadInputFile function as returning a bool or other value if it might fail, this way the main function can check to see if everything went OK.
0
 
LVL 1

Expert Comment

by:travd
ID: 9854452
I suppose also that your question could be interpreted as "how do I pass the filename in as a parameter to the main() function?"

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)
0
 

Author Comment

by:dkim18
ID: 9854603
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_data_file);

}
----------------------------
this gives me error like this:

AnalyzeOutput.cpp: In function `void ReadInputFile(basic_string<char,string_char
_traits<char>,__default_alloc_template<false,0> >)':
AnalyzeOutput.cpp:22: no matching function for call to `ifstream::ifstream (stri
ng &)'
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.2/../../../../include/g++-3/fst
ream.h:61: candidates are: ifstream::ifstream()
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.2/../../../../include/g++-3/fst
ream.h:62:                 ifstream::ifstream(int)
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.2/../../../../include/g++-3/fst
ream.h:63:                 ifstream::ifstream(int, char *, int)
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.2/../../../../include/g++-3/fst
ream.h:65:                 ifstream::ifstream(const char *, int = ios::in, int =
 436)
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.2/../../../../include/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.)

0
 
LVL 1

Accepted Solution

by:
travd earned 100 total points
ID: 9854891
You need to declare the name as:

char generated_data_file[] = "input.dat";

(array sizes go after the variable name)
0
 

Author Comment

by:dkim18
ID: 9855075
Thank you pointing out my mistake.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ dll will not run on machine without VS installed 10 88
Which Linux flavors will this run on? 6 96
C++ mouse_event mouse look 7 99
VS2015 Redefinition errors 4 72
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question