inFile.open() options

Jul 7, 2008 at 1:12pm
Alright, i've got a couple problems to work out. I'm writing a section of a program that requires it to back up data stored in arrays within the program. So what i'm trying to do is create a seperate directory elsewhere on the harddrive that will contain the backed up files.

I need 2 things:
1. I need to be able to open the files within that directory
2. I need the date the back up file was created in the filename

I'll tackle number one first...
I tried simply doing

inFile.open("C:\My Documents\Backup\backup.txt", ios::in | ios:: binary);

and it doesn't like having that whole address in there. Is there some way to change the current directory or somehow tell it to open a file that is not in the same directory as the program itself?

and problem number two...

I want to create a text file in the above directory in which to outFile the data to, but i want the date it was backed up in the file name (even if it's like this: 07072008)
How do i go about doing that?
Jul 7, 2008 at 2:27pm
First, this code can't work:
 
inFile.open("C:\My Documents\Backup\backup.txt", ios::in | ios:: binary);


because the \ is the escape sequence character. It tells the compiler that the character after the \ is a code. Like \n is a newline and not an n.

To get an actual backslash, you need to force it:
 
inFile.open("C:\\My Documents\\Backup\\backup.txt", ios::in | ios:: binary);


The second problem to get the backup date into the file name, use a string object and build the date into the string. Then open the file using the string object.

1
2
3
4
5
6
7
8
9
string theDate;
cin >> theDate;        //carefully enter 070722008
string str;
str = "C:\\My Documents\\Backup\\backup.txt";
str += "\\";
str += "BackupAsOf";
str += theDate;
ifstream infile;
infile.open(str.c_str(), ios::in | ios::binary);

Topic archived. No new replies allowed.