Sorry I missed the backslashes in your strings... Yeah, you've got to double them.
You can actually open a file for both reading and writing, you just have to reset the read pointer before reading.
The simplest way to think of a file is as an array of bytes (or
chars) on disk. So, when you first
create a file, the
file pointer is addressing the first byte in the file.
After writing to the file, you have updated the file pointer:
myfile << "Hi!";
becomes:
If you now want to
read from the file, remember that the file pointer is still at the
end of the file. You first have to reset it to the beginning of the file.
myfile.seekg( 0 );
becomes:
Now you can read what you wrote:
char c = myfile.get();
becomes:
where
c contains the value
'H'.
The only 'gotcha' that you have to remember with streams is that the read pointer and the write pointer aren't necessarily separate entities (even though they are supposed to be for C++), so if you change one then assume that the other is lost. In other words, after you read, use
seekp() before you write, and after you write, use
seekg() before you read.
As for folders, it is a convenience of the operating system's user interface that certain user commands allow you to create folders and files at the same time. In C++ code, you have to remember that they are actually separate actions, so you must verify or create each directory before you can create a file.
The typical C and C++ function to do these things, while non-standard C and C++, but are POSIX functions, are:
1 2 3 4 5
|
#include <sys/stat.h>
#include <sys/types.h>
int stat(const char *restrict path, struct stat *restrict buf);
int mkdir(const char *path, mode_t mode);
|
http://www.opengroup.org/onlinepubs/009695399/functions/stat.html
http://www.opengroup.org/onlinepubs/000095399/functions/mkdir.html
Also, there is the Boost Filesystem library, which makes handling these things a breeze.
http://www.boost.org/doc/libs/1_35_0/libs/filesystem/doc/index.htm
Hope this helps.