| | |
Passing a filename to a function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 1
Reputation:
Solved Threads: 0
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!
Could anybody give me a clue how to do this please?
Thanks!
You can do this by passing a pointer to char or a string.
C++ Syntax (Toggle Plain Text)
#include <string> void yourFunction(const char * filename); //we don't want to modify so it's const void yourFunction(string filename);
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)
std::string filename = "my_file.txt"; std::ifstream infile(filename.c_str());
![]() |
Similar Threads
- passing fstream to a function and searching for a string (C++)
- function files (C++)
- Passing An Connecting String (ASP.NET)
- Generic method parser / invoker (C#)
- Function Style: writeLog(...) (IT Professionals' Lounge)
- error: too many arguments to function `mysql_query' (C)
- Windows programming - C - Save file function (C++)
- Passing char * to function and populating inside (C++)
Other Threads in the C++ Forum
- Previous Thread: Suggest Project
- Next Thread: help me
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






