Thread: Creating files in specific directories

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    145

    Unhappy Creating files in specific directories

    I can write information to files, but I want to be able to save the resulting file to various folders. How do I do this? An example of my code is below. Thanks in advance for any help!

    -----------------------
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>

    int main()
    {
    string Name,
    School;

    ofstream a_file("Info.doc");
    a_file<< "Your name is : " << Name <<endl;
    a_file<< "You go to " << School <<endl;
    a_file.close();

    return(0);
    }
    -----------------------

    I would like to be able to save Info.doc (or whatever my file name is) in a different folder, anything but the base folder that I ran the program from (where the file normally creates). Any help is appreciated.

    Kyoto Oshiro
    http://www.angelfire.com/realm2/horizon/files.html
    Horizon Games

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    ofstream a_file("C:\\NameOf\\Other\\Folder\\Info.doc");
    Of course, make sure the the folder/directory you are going to create the file in first exists. Otherwise you will need to go through some steps to create it (the directory) in your program.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    145

    Smile Thanks a lot

    Oh, alright then. Sounds easy enough. Thanks for the help!


    Kyoto Oshiro
    http://www.angelfire.com/realm2/horizon/files.html
    Horizon Games

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Directories and files using dirent.h
    By totalnewbie in forum C Programming
    Replies: 6
    Last Post: 11-19-2008, 05:10 PM
  2. creating directories
    By hiya in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2005, 12:08 AM
  3. creating .DAT files
    By breed in forum C Programming
    Replies: 3
    Last Post: 11-30-2001, 09:09 AM
  4. Creating a prog to edit files with certin extention
    By destroyer32428 in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2001, 03:08 AM
  5. creating make files
    By Unregistered in forum Linux Programming
    Replies: 4
    Last Post: 09-22-2001, 01:58 AM