How can i create a text file in a certain directory using ofstream?
Also, how can i access a text file in a certain directory using ifstream?
Any help is Apreciated. Thx.
~BC17
Printable View
How can i create a text file in a certain directory using ofstream?
Also, how can i access a text file in a certain directory using ifstream?
Any help is Apreciated. Thx.
~BC17
Code:ifstream ReadFile;
ofstream WriteFile;
//Open a file in an absolute folder
ReadFile.open("C:\\MyFolder\\MyFile.ext", ios::in);
//Open a file in a subfolder (from the one the exe is in)
WriteFile.open("MyFolder\\MyFile.ext", ios::out);
For WriteFile, shouldn't it be ios::out?
Thankyou very much. This knowledge will help my with my database program.
for some reason the file is not being createdCode:ofstream test;
test.open("C:Program Files\\Phonebook\\filename.txt", ios::out);
test<<data; //inputs a string into the file data is a character array
test.close();
fstream test1;
test1.open("C:Program Files\\Phonebook\\filename.txt", ios::in);
test1>>str; //retrieves string inputted to the file and stores it
//as str( a character array)
test.close();
Anyone know why?
err sorry the \\ is in my program code(just not in the post) still doesn't work tho
Also, is the ios necessary?
Code:ofstream test;
test.open("C:\\Program Files\\Phonebook\\filename.txt", ios::out);
test<<data; //inputs a string into the file data is a character array
test.close();
fstream test1;
test1.open("C:\\Program Files\\Phonebook\\filename.txt", ios::in);
test1>>str; //retrieves string inputted to the file and stores it
//as str( a character array)
test.close();
Try this and see what happens:
Code:ofstream test;
test.open("C:\\Program Files\\Phonebook\\filename.txt", ios::out);
if(test.fail())
{
cout << "Unable to save!" << endl;
return 0;
}
test<<data; //inputs a string into the file data is a character array
test.close();
fstream test1;
test1.open("C:\\Program Files\\Phonebook\\filename.txt", ios::in);
if(test1.fail())
{
cout << "Unable to load!" << endl;
return 0;
}
test1>>str; //retrieves string inputted to the file and stores it
//as str( a character array)
test.close();
unable to save
Hm weird, outfiles rarely fail since if they exists they are overwritten, and if they don't exists they are created. Are you sure the folder (Program Files/Phonebook) exists?Quote:
Originally posted by bc17
unable to save
It's there, and if it wasn't then shouldn't it be created anyways?
I took out the ios::out and the ios::in, then changed the file and folder names to be 8 characters or under(im using Borland Turbo C++ ver 3.0, a dos based compiler). That solved the problem
Code:test.open("C:\\PB\\filename.txt");
if(test.fail())
{
cout << "Unable to save!" << endl;
return 0;
}
test<<data; //inputs a string into the file data is a character array
test.close();
fstream test1;
test1.open("C:\\PB\\filename.txt");
if(test1.fail())
{
cout << "Unable to load!" << endl;
return 0;
}
test1>>str; //retrieves string inputted to the file and stores it
//as str( a character array)
test.close();