954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

edit, create, delete, read file in a different directory??

I know i can use fstream to create, edit and read files in c++, i also believe there is a different way wich i am unfamiliar with. but is it possible to work with files NOT in the same directory as the program? like if the program is in C:\programfiles but i want it to append something to a file in C:\filesiwanttoalter. or what ever directory??

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 

You should be able to just specify the entire path and file name as the parameter.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

so

#include <iostream>
#include <fstream>

using namespace std;

Int main () {

ofstream a;
a.open ("C:\programs\fileneedschanges", ios::app);
a << "adding this to end of fileneedschanges";
a.close ();
return 0;
}

would do the trick? i was pretty sure it would not :-) btw why does a.close (); need the "()" when a.open does not??

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 

can you not see the () on open? Its just you have two parameters inbetween them :P

You will need to escape that '\' characters
"C:\\programs\\fileneedschanges"
Then t shold work fine

Chris

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

I can't believe i asked that :D of course there is the ( and ) :D thanks

So double slashes? \\ and that's it? does it need a way to see the difference between the folders and the file, or does it just tawke the last one for a file?
EDIT: is cursing allowed in here? if not i am sorry.

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 

We'll give it a miss if you mark the thread as solved :P

Chris

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

aight i've tried it now, as expected it works :D before i mark it as solved though, would you please explain to me why i needs to use double slash?:D

^^will you forgive the question or the swearing when i mark it solved? :P

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 

Sorry I missed the question. '\' is used to escape characters such as '\n' is a newline as you may well no or '\a' is a bleep. Because it's a special escape character in string you must escape it if you wish to use one. Hence the double slash.

Chris

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

Ahh, that makes sense. i knew about the \n (yeah i know, that makes me a genious ;-) )
but i didn't realise that was it :D well then that was it, thank you for you patience.

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You