Thread: saving to another directory

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    106

    saving to another directory

    i have recently learned how to use files in c++, and i was wondering if anyone could explain how to save files to a directory other than the one that the program is in.
    if my program is in the folder "program", and i want to save all the files under "program\saves", does anyone know how to do this?
    thanks a lot in advance, and this may be a newb question so thanks.

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    to go to other directories use chdir command in dir.h create the file in this directory and save it. see the dir.h for more directory related functions

    some code help..

    ---
    char path[20];
    ofstream fout;

    cout<<"ENTER PATH : ";
    cin.getline(path,20,'\n');
    chdir(path);

    fout.open("FILE1");
    fout<<"FILE SAVED IN "<<path;
    fout.close();
    ---

    i haven't tested this.

    hope this helps


    PS, Hey vVv i used getline..
    -

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    cool thanks a lot for your help, ill check it out.
    thanks!

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

    Question

    I have seen a lot of people doing file I/O differently than me. I posted some code as an example below, and I was wondering if there is a better way at all.


    Lets say I was trying to output a name to Name.txt, then call it back and output it to the user. I would use the code below...

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

    int main()
    {
    string Name = "Kyoto";

    ofstream a_file("Name.txt");
    a_file<< Name << endl;
    a_file.close();

    ifstream b_file("Name.txt");
    b_file>> Name;
    b_file.close();

    clrscr();
    gotoxy(1,2);

    cout<< "Your name is: " << Name << "." << endl;

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

    Now that is how I do my file I/O, but I often see fout and things like that. Which way is better? Any ideas?


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

  5. #5
    Unregistered
    Guest
    a name is just a name. As long as it is clear what you are doing, the name of the object itself doesn't matter.

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    #include <windows.h>

    system("mkdir saves");
    char string[2] = "hi";

    ofstream fout("saves/names.txt");
    fout<< string << endl;
    fout.close();

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    alright, thanks a lot everyone, i got it all to work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  3. Replies: 6
    Last Post: 07-30-2003, 03:08 AM
  4. Directory reading trouble
    By samGwilliam in forum Linux Programming
    Replies: 0
    Last Post: 03-10-2002, 09:43 AM
  5. The Site Directory
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-22-2002, 08:19 PM