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??

Recommended Answers

All 8 Replies

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

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??

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

commented: :) +8
commented: well, it solved my problem +1

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.

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

Chris

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.