| | |
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 |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






