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
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);
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
For WriteFile, shouldn't it be ios::out?
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
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();
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
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?Originally posted by bc17
unable to save
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
It's there, and if it wasn't then shouldn't it be created anyways?
Last edited by bc17; 11-24-2002 at 01:05 PM.
I took out the ios:ut 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();
Last edited by bc17; 11-25-2002 at 11:26 AM.