By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 980 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

File name as a variable

P: 8
I was wondering if anyone could tell me how i can use a vairable as a file name such as.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3.  
  4. ifstream file;
  5. string file = "test.dat";
  6.  
  7. file.open(file.c_str())
  8.  
  9.  
when i do it wont open the file.
Mar 8 '07 #1
Share this Question
Share on Google+
2 Replies


Ganon11
Expert 2.5K+
P: 3,652
You shouldn't name the string variable and the ifstream variable the same thing. Try

Expand|Select|Wrap|Line Numbers
  1. ifstream file;
  2. string filename = "test.dat";
  3. file.open(filename.c_str());
Also, make sure test.dat is an existing file.
Mar 8 '07 #2

P: 8
i have.
Expand|Select|Wrap|Line Numbers
  1. ifstream input;
  2. string file = "test.txt";
  3. input.open(file.c_str());
  4.  
and the file exists. i put it in there myself.
Mar 8 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.