Opening A Text File with a variable argument
Nov 7, 2011 at 6:33am UTC
I'd like to take in a string from the user and open a text file based on the user input: Here's What I have:
1 2 3 4 5 6 7 8 9 10 11 12 13
void open(std::vector<std::string> existing_files, std::string name){
std::string address("C:/Documents and Settings/Lauren/My Documents/Visual Studio 2008/Projects/Marketing/Marketing/Marketing/" +name+".txt" );
const char * choice;
choice = new const char [address.size()+1];
strcpy(choice, address.c_str());
std::cout << choice << std::endl;
std::ifstream data(choice);
if (data.is_open()){
std::cout << "Hello" << std::endl;
std::cin >> num;
}
};
The problem is that I cannot modify a const char (variable)...ifstream requires a const char as its main argument. How do I convert the string 'address' to an argument that can be used by ifstream?
Nov 7, 2011 at 8:31am UTC
why don't you use address.c_str() which returns const char*pointed by string object.
Topic archived. No new replies allowed.