Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,766 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,598 people online right now. Registration is fast and FREE... Join Now!




C++ File placement

 
Reply to this topicStart new topic

C++ File placement, creating a directory for files

Antiokus
6 Feb, 2007 - 11:33 AM
Post #1

D.I.C Head
**

Joined: 6 Sep, 2006
Posts: 128



Thanked: 1 times
My Contributions
I'm working on a program (not homework) at the moment that reads and writes to multiple files. My question is when I create new files (and read from them) is there a way to place/ read from a path instead of the default folder where the .exe is stored?

This post has been edited by Antiokus: 6 Feb, 2007 - 11:33 AM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: C++ File Placement
6 Feb, 2007 - 11:52 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Yes, just provide the path with filename to access the file from any folder.

You will need to use two backslashes instead of one so that you will get the correct path. Otherwise you will get errors because the compiler will think you are trying to access an escape sequence that doesn't exist.

For exampe:
CODE

inputStream.open("C:\\Documents and Settings\\Administrator\\Desktop\\myFile.exe");


BTW, how goes it at school?
User is offlineProfile CardPM
+Quote Post

horace
RE: C++ File Placement
6 Feb, 2007 - 11:53 AM
Post #3

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
QUOTE(Antiokus @ 6 Feb, 2007 - 07:33 PM) *

I'm working on a program (not homework) at the moment that reads and writes to multiple files. My question is when I create new files (and read from them) is there a way to place/ read from a path instead of the default folder where the .exe is stored?

you have to put the path (relative or absolute) in, e.g. unix
CODE

  outfile.open ("/home/zg/testdata/test.txt",  ios::binary | ios::out);

or in Windows
CODE

  outfile.open ("c:\\zg\\testdata\\test.txt",  ios::binary | ios::out);


User is offlineProfile CardPM
+Quote Post

Antiokus
RE: C++ File Placement
6 Feb, 2007 - 11:55 AM
Post #4

D.I.C Head
**

Joined: 6 Sep, 2006
Posts: 128



Thanked: 1 times
My Contributions
Awesome, thanks!

I must have messed up trying it. I kind of figured that you could just give it a path name.

Eh, it's going alright. Luckily we got a few good guys this quarter. Your old computer is now the "hang out" spot for William and everyone, so Dan is going to try sitting out there one day and see how that works to keep them in line.

Must make ITT look even better, right?
User is offlineProfile CardPM
+Quote Post

Antiokus
RE: C++ File Placement
6 Feb, 2007 - 12:03 PM
Post #5

D.I.C Head
**

Joined: 6 Sep, 2006
Posts: 128



Thanked: 1 times
My Contributions
OK looks like I need a little more help here:

fout.open(user.c_str());

that's what I was originally using to create the file (user is a string).
How do I append a directory onto that? Could I just concat the value of user into a string that holds the entire path name?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: C++ File Placement
6 Feb, 2007 - 12:07 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Yes, or you could concatenate inside the fout.open constructor.
CODE

fout.open("C:\\Documents and Settings\\Administrator\\Desktop\\" + user.c_str());

User is offlineProfile CardPM
+Quote Post

Antiokus
RE: C++ File Placement
6 Feb, 2007 - 12:14 PM
Post #7

D.I.C Head
**

Joined: 6 Sep, 2006
Posts: 128



Thanked: 1 times
My Contributions
its not liking concat inside of fout.open():

"invalid operands of types 'const char[62]' and 'const *char' to binary 'operator+'"


User is offlineProfile CardPM
+Quote Post

horace
RE: C++ File Placement
6 Feb, 2007 - 12:41 PM
Post #8

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
QUOTE(Antiokus @ 6 Feb, 2007 - 08:14 PM) *

its not liking concat inside of fout.open():

"invalid operands of types 'const char[62]' and 'const *char' to binary 'operator+'"

you concatenate the strings then extract the char * for open(), e.g.
CODE

  string filename="test.txt";
  outfile.open (("c:\\zg\\testdata\\" + filename).c_str(),  ios::binary | ios::out);

User is offlineProfile CardPM
+Quote Post

Jayman
RE: C++ File Placement
6 Feb, 2007 - 12:49 PM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Whoops, I forgot parenthesis. Thanks for the correction horace.
User is offlineProfile CardPM
+Quote Post

Antiokus
RE: C++ File Placement
6 Feb, 2007 - 12:54 PM
Post #10

D.I.C Head
**

Joined: 6 Sep, 2006
Posts: 128



Thanked: 1 times
My Contributions
Sure enough, working perfectly

thanks guys.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 06:21AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month