File Input/Output Path Directory
Hmm i can't believe I'm having problems with this...
but anyways:
void Tmain_form::write_to_file()
{
ofstream filename("chris.txt"); //opens file chris.txt
filename << "A"; //writes 'A' to chris.txt
filename.close(); //closes file.
}
code above saves the letter 'A' to a file named 'chris.txt'.
But it saves the file 'chris.txt' in the same directory where the .cpp file is saved.
i want to be able to declare the path like below:
void Tmain_form::write_to_file()
{
ofstream filename("c:\program files\chris.txt");
filename << "A";
filename.close();
}
but the above code doesn't work. It just saves in the dir where the .cpp is.... I've also tried:
void Tmain_form::write_to_file()
{
AnsiString path = "c:\program files\chris.txt";
ofstream filename(path);
filename << "A";
filename.close();
}
but i get an error cause i'm guessing the function
ofstream filename();
can't pass an AnsiString variable.. but i tried char and that doesn't work either... anyone help me out with this?
thanx man that was the problem
Thanks yah fixed the problem... forgot all about the backslash being a character escape key....
one more question for anyone who can answer it...
void Tmain_form::write_to_file()
{
ofstream filename("c:\\chris.txt");
filename << "A";
filename.close();
}
works but.....
void Tmain_form::write_to_file()
{
AnsiString path = "c:\\chris.txt";
ofstream filename(path);
filename << "A";
filename.close();
}
i've tried declaring path as type 'String', 'Char', 'AnsiString'
and none seem to work... something i'm doing wrong?
i also tried cause i running outta ideas:
void Tmain_form::write_to_file()
{
AnsiString path = "c:\\chris.txt";
ofstream filename("\"" + path + "\"");
filename << "A";
filename.close();
}