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

Path of file :filename variable

P: 11
Hi friends,

I am opening and reading a file and writing it in a new file.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.  
  4.   ifstream inputFile("C:\\TEMP\\test.txt");
  5.   ofstream outputFile;
  6.  
  7.   outputFile.open("C:\\TEMP\\test3.txt");
  8.  
But I want my new file (outputfile) as a variable

I have tried by adding strings but it doesnt work
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.  
  4. ifstream inputFile("C:/TEMP/test.txt");
  5.   ofstream outputFile;
  6.   cout<<"Enter the outputfile name: "<<endl;
  7.   cin>>file;
  8.   string newfile=  ("C:/TEMP/"+ file);
  9.   outputFile.open("newfile");
  10. }
While using the above function the file has not been created and the data is not written.

Can anyone tell me how I can give the input of file name with the path specified?

Thanks in advance,

sheriff
May 30 '07 #1
Share this Question
Share on Google+
8 Replies


ilikepython
Expert 100+
P: 844
Hi friends,

I am opening and reading a file and writing it in a new file.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.  
  4.   ifstream inputFile("C:\\TEMP\\test.txt");
  5.   ofstream outputFile;
  6.  
  7.   outputFile.open("C:\\TEMP\\test3.txt");
  8.  
But I want my new file (outputfile) as a variable

I have tried by adding strings but it doesnt work
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.  
  4. ifstream inputFile("C:/TEMP/test.txt");
  5.   ofstream outputFile;
  6.   cout<<"Enter the outputfile name: "<<endl;
  7.   cin>>file;
  8.   string newfile=  ("C:/TEMP/"+ file);
  9.   outputFile.open("newfile");
  10. }
While using the above function the file has not been created and the data is not written.

Can anyone tell me how I can give the input of file name with the path specified?

Thanks in advance,

sheriff
You passed a string literal to open(). That means that it is trying to open a file called "newfile". Pass the actual value that the variable holds (without the quotes).
May 30 '07 #2

P: 11
You passed a string literal to open(). That means that it is trying to open a file called "newfile". Pass the actual value that the variable holds (without the quotes).

thx "ilike python"?

but without quotes

compiler says syntax is not right (no matchhing function)
May 30 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
string newfile= ("C:/TEMP/"+ file);
Did you try changing this to

string newfile = "C:/TEMP/" + file;
May 30 '07 #4

ilikepython
Expert 100+
P: 844
thx "ilike python"?

but without quotes

compiler says syntax is not right (no matchhing function)
What's the error? Maybe it wants a c-string. Try:
Expand|Select|Wrap|Line Numbers
  1. outputfile.open(newfile.c_str());
  2.  
May 30 '07 #5

P: 11
What's the error? Maybe it wants a c-string. Try:
Expand|Select|Wrap|Line Numbers
  1. outputfile.open(newfile.c_str());
  2.  

hi ilikepython,

im not getting this c_str()function

im not good at this pointer things

can u tell me a little more abt y r u implying this here.. if possible?

by the way cstring is not working with my c++ compiler

as it is for visual c++ i hope!!!
May 31 '07 #6

P: 11
What's the error? Maybe it wants a c-string. Try:
Expand|Select|Wrap|Line Numbers
  1. outputfile.open(newfile.c_str());
  2.  

thx python it is working

thx a lot
Jun 1 '07 #7

ilikepython
Expert 100+
P: 844
hi ilikepython,

im not getting this c_str()function

im not good at this pointer things

can u tell me a little more abt y r u implying this here.. if possible?

by the way cstring is not working with my c++ compiler

as it is for visual c++ i hope!!!
That function turns a string object into a char array. That is needed because open() function wants a char array, so if you put a string it will complain. But since you put the c_str() function, that makes it into a char array, so it works.
Jun 1 '07 #8

ilikepython
Expert 100+
P: 844
thx python it is working

thx a lot
I'm glad to help you
Jun 1 '07 #9

Post your reply

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