Passing a filename to a function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 1
Reputation: adarrell is an unknown quantity at this point 
Solved Threads: 0
adarrell adarrell is offline Offline
Newbie Poster

Passing a filename to a function

 
0
  #1
Jan 30th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 16
Reputation: venomxxl is an unknown quantity at this point 
Solved Threads: 2
venomxxl's Avatar
venomxxl venomxxl is offline Offline
Newbie Poster

Re: Passing a filename to a function

 
0
  #2
Jan 30th, 2009
You can do this by passing a pointer to char or a string.

  1. #include <string>
  2. void yourFunction(const char * filename); //we don't want to modify so it's const
  3. void yourFunction(string filename);
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,853
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Passing a filename to a function

 
0
  #3
Jan 30th, 2009
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:

  1. std::string filename = "my_file.txt";
  2. std::ifstream infile(filename.c_str());
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC