943,773 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2062
  • C++ RSS
Jan 30th, 2009
0

Passing a filename to a function

Expand Post »
Hi, I have been trying for several hours without success to write a function that takes a filename, opens the corresponding file, populates a std::vector<double> with values from the file, and returns the std::vector<double>. I can't get past the passing the filename stage unfortunately, despite reading about this here there and everywhere online.

Could anybody give me a clue how to do this please?

Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
adarrell is offline Offline
1 posts
since Jan 2009
Jan 30th, 2009
0

Re: Passing a filename to a function

You can do this by passing a pointer to char or a string.

C++ Syntax (Toggle Plain Text)
  1. #include <string>
  2. void yourFunction(const char * filename); //we don't want to modify so it's const
  3. void yourFunction(string filename);
Reputation Points: 34
Solved Threads: 7
Junior Poster in Training
venomxxl is offline Offline
72 posts
since Jan 2009
Jan 30th, 2009
0

Re: Passing a filename to a function

Also, if you're using std::strings for a filename you'll have to convert them to const char* for ifstream. The c_str() function will do that for you:

C++ Syntax (Toggle Plain Text)
  1. std::string filename = "my_file.txt";
  2. std::ifstream infile(filename.c_str());
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Suggest Project
Next Thread in C++ Forum Timeline: help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC