Thread: Creating a file in a certain directory

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    30

    Creating a file in a certain directory

    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

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    30
    Thankyou very much. This knowledge will help my with my database program.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    30
    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();
    for some reason the file is not being created
    Anyone know why?

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    30
    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();

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    30
    unable to save

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by bc17
    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?
    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.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    30
    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.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    30
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Replies: 9
    Last Post: 03-03-2006, 10:11 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM