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

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




Working with files

 
Reply to this topicStart new topic

Working with files, Writting to a specific directory

mattman059
24 Nov, 2007 - 01:38 PM
Post #1

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 361


Dream Kudos: 175
My Contributions
I would like to reference a post that has already been answered in hopes that it will help answer my question

Working with Files Link

Now, in that post, the member asks about using a file directory with the system() command

I would like to create a file in a specific directory: Heres what I have :

CODE


ofstream file("<path to my desktop>\file.bin",ios::out|ios::binary);



the <path to my desktop> is of course not actual code, but the actual path to my desktop.

now when I run the program nothing obvious goes wrong, no errors and whatnot. But when i inspect my desktop there is no file.bin....BUT if i go to the folder where my project is, I notice that there is a file there

(assume my desktops path is C:\Documents and Settings\MyDesktop\Desktop)

with the previous assumption I get the following:

CDocumentsandSettingsMyDesktopDesktopstack.bin

blink.gif Not exactly what I wanted...Any suggestions?

This post has been edited by mattman059: 24 Nov, 2007 - 01:40 PM
User is offlineProfile CardPM
+Quote Post

Bench
RE: Working With Files
24 Nov, 2007 - 02:32 PM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 684



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
in C and C++, the backslash (\) character is a special one which denotes an Escape sequence. An escape sequence is for special characters which don't have an ordinary representation (such as a tab, a new-line, etc). or that have a special meaning within C/C++ string literal grammar, such as a double-quote character (Which usually denotes a string literal on its own).

if you wish to include a backslash character in your code as part of a string, then you must use the escape sequence for the backslash character, which is a double backslash; \\

In short, your directory paths must look something like this
"C:\\MyFolder\\MySubFolder\\Myfile.txt"

In Windows, sometimes you have spaces in directory paths, which require you to use double-quotes as part of the path string. So, if you wish to use double-quote character within a string, you have the escape character for a double quote, which is \"

Have a look at this link for more escape sequences
http://www.cppreference.com/escape_sequences.html


edit - The alternative is to type your directory paths using the forward-slash character, a-la *nix. This often works in Windows too
"C:/MyFolder/MySubFolder/Myfile.txt"

This post has been edited by Bench: 24 Nov, 2007 - 02:53 PM
User is offlineProfile CardPM
+Quote Post

mattman059
RE: Working With Files
25 Nov, 2007 - 02:32 PM
Post #3

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 361


Dream Kudos: 175
My Contributions
Thanks, That worked great for my XP machine, now i'm trying to use the same program on Vista, and i followed the same procedure as above but no luck...any idea how Vista uses escape sequences
User is offlineProfile CardPM
+Quote Post

Bench
RE: Working With Files
25 Nov, 2007 - 02:41 PM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 684



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
escape sequences are part of C, and therefore are completely O/S independent. The problem will be something else - Double check that the file is where you expect it to be on your vista box (You could try to see if you can reach the file using the Vista command prompt. Anything that the command prompt can't do, your program won't be able to do either)

This post has been edited by Bench: 25 Nov, 2007 - 02:45 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Working With Files
25 Nov, 2007 - 02:57 PM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
Vista has that irritating "virtual store" which will cause problems. Basically it keeps your program from directly accessing the file structure...
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:50PM

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