Menu

Writing to a file - wrong directory

aniak17
2003-06-29
2012-09-26
  • aniak17

    aniak17 - 2003-06-29

    I have written this program:

    ------------------------------

    #include <fstream>
    #include <iostream>
    #include <stdio.h>

    int main()
    {
      ofstream SaveFile("testing.txt");
      SaveFile << "Hello World, testing file writing";
      SaveFile.close();

      cout << "Press ENTER to exit..." << endl;
      getchar();
      return 0;
    }

    -------------------------------

    It works fine, except the new file that is written to ("testing.txt") is created in my Dev-C++ directory (where I installed the actual Dev-C++ application) - instead in my current working directory (where the program's .exe file is).

    This is a huge problem for me, as I have wrtten a much larger program which does exactly the same thing.

    Is there a setting in Dev-C++ that I need to change?

    Please help - this is very important to me.

    CHEERS - ANIA

     
    • Raphael Saint-Pierre

      you could get the command line which was used to launched your program ; there's the filename and directory in it : it is the first argument of your main function ; you just have to declare

      int main( int argc, char * argv[] )

      and then locate the position of the last '\' in argv[0] ; all the stuff before it is your path.

      Petit_PimoOosE

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.