Not sure where to save file

Dec 16, 2008 at 5:11am
Hey guys.

When writing a program that reads from a file, where do I save the file so the program can read it?

Thanks for your help in advance.
Dec 16, 2008 at 5:15am
You save it in the same directory as the program.
E.G. If the program is in Desktop, the file would need to be in Desktop as well.

I think you can specify the location though, but I'm not sure how.
Dec 16, 2008 at 12:50pm
ofstream write("file.txt"); the file is saved in the same location of the program
ofstream write("C:\\Users\\Bazzy\\Documents\\file.txt"); the file is saved in the given directory
(the same with ifstreams)

(edit)
Don't post twice! http://www.cplusplus.com/forum/general/6418/
Last edited on Dec 16, 2008 at 12:57pm
Dec 16, 2008 at 1:29pm
As an aside, the Windows kernel understands the forward slash as equivalent to the backslash. You could use "C:/Users/Bazzy/Documents/file.txt" and it would produce the expected result.

I still don't know why the UI doesn't allow forward slashes. It'd make my life a lot easier.
Last edited on Dec 16, 2008 at 1:29pm
Dec 16, 2008 at 1:53pm
It depends on the nature of the file you are going to save, and the OS.

Application configuration data

POSIX (Linux, Unix, etc)
Either create a single file like "~/.myapprc"
or make a subdirectory like "~/.myapp" with files inside it.

Windows (all versions)
The same thing, except you cannot use "~/" to represent the user's home directory. (And you don't need to prefix the filename with a period to hide it.) You must get the directory name from the Registry:
- Key "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
- Value "AppData" (type: REG_EXPAND_SZ)
It is an expandable value, remember. Use the ExpandEnvironmentStrings() function on it before you use it.


Temporary files
Wherever the environment variable "TEMP" (or "TMP", if the first doesn't exist) indicates. You can also #include <cstdio> and use the tmpfile() or tmpnam() functions, which work properly on any system.

User data
Wherever the user indicates. Either give the user an option (open/save dialogues) or use the current working directory.

Hope this helps.
Topic archived. No new replies allowed.