473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Path of file :filename variable

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
8 8116
ilikepython
844 Expert 512MB
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
sheriff
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
4,677 Expert Mod 4TB
string newfile= ("C:/TEMP/"+ file);
Did you try changing this to

string newfile = "C:/TEMP/" + file;
May 30 '07 #4
ilikepython
844 Expert 512MB
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
sheriff
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
sheriff
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
844 Expert 512MB
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
844 Expert 512MB
thx python it is working

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

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

Similar topics

70
by: Michael Hoffman | last post by:
Many of you are familiar with Jason Orendorff's path module <http://www.jorendorff.com/articles/python/path/>, which is frequently recommended here on c.l.p. I submitted an RFE to add it to the...
5
by: Paul | last post by:
Hi, Using the code below I can extract the filename from a path but I would like to know how to get just the path too. So if the full path is "C:\A Long Time Ago\In A Galaxy\Far Far...
34
by: Reinhold Birkenfeld | last post by:
Hi, the arguments in the previous thread were convincing enough, so I made the Path class inherit from str/unicode again. It still can be found in CVS:...
2
by: Rob Cowie | last post by:
Hi, Given a string representing the path to a file, what is the best way to get at the filename? Does the OS module provide a function to parse the path? or is it acceptable to split the string...
4
by: Mark Healey | last post by:
I need to get the filename and path of a program file from within that file. I've tried the $0 variable but that doesn't work. It gives the path from the command that called the file. This is...
2
by: Csharper95 | last post by:
I want to get the file name and path from a SaveFileDialog (for a 'save as' operation) after the user has saved the information under a new file name (and presumably under a new path) ? I want...
3
by: Raed Sawalha | last post by:
I'm getting the file path via webservice using following code:- FileInfo fInfo = new FileInfo(filename); return fInfo.FullName; the return file backed as C:\test\images\1.jpg in application...
3
by: Darrel | last post by:
using Request.FilePath will return the entire file path: directory/directory/myfile.aspx Is there a built function in ASP.net to grab just the filename (myfile.aspx) or do I need to write...
11
by: cdkorzen | last post by:
I'm sorry if this is a rehash, but all I see is the same info. Here's my debacle: I CAN get the PATH_INFO to work. With ANYTHING but ASP. Python, Perl, Cmd files... works fine. ASP can't...
34
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.