creating a file in the same directory as the executable program

This is a discussion on creating a file in the same directory as the executable program within the C Programming forums, part of the General Programming Boards category; I'm creating/reading/writing a file in my program, but initially the file will have to be created before it is used. ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    15

    creating a file in the same directory as the executable program

    I'm creating/reading/writing a file in my program, but initially the file will have to be created before it is used.

    Do I create a file with fopen? If not, what function do I use?

    Also, what directory path do I specify if I want to create the file in the same folder as the .exe file of the program that is creating the file?

    Thank you.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Which OS/Compiler are you using?

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    Win XP/ Dev-C++

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Yes, fopen() for write will create the file.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    This code should work for you. It will create the file MyFile.ext in the same directory as your executable. Make sure and include <windows.h>. Another option is to use the executable path given to your main() or WinMain() function.
    Code:
    char buff[MAX_PATH], *p;
    FILE *f;
    GetModuleFilePath(NULL,buff,MAX_PATH);
    p = strrchr(buff, '\\');
    if(p)
    {
       strcpy(p+1,"MyFile.ext");
    }
    f = fopen(buff,"w");

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    Just call fopen with the file name and the "w" parameter and it will create/overwrite a file in the executable's directory...

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    Quote Originally Posted by bithub
    This code should work for you. It will create the file MyFile.ext in the same directory as your executable. Make sure and include <windows.h>. Another option is to use the executable path given to your main() or WinMain() function.
    Code:
    char buff[MAX_PATH], *p;
    FILE *f;
    GetModuleFilePath(NULL,buff,MAX_PATH);
    p = strrchr(buff, '\\');
    if(p)
    {
       strcpy(p+1,"MyFile.ext");
    }
    f = fopen(buff,"w");
    is there anyway to do this without using the WinAPI? How can I get the directory through standard command-based C?

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Just call fopen with the file name and the "w" parameter and it will create/overwrite a file in the executable's directory...
    No, it will create the file in the current working directory which could be different than the executable's directory.

    is there anyway to do this without using the WinAPI? How can I get the directory through standard command-based C?
    Yes. Use the directory passed to main(argc, argv).

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    No, it will create the file in the current working directory which could be different than the executable's directory.
    I think you're using MS Visual C++ too much...

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    A related thread elseweb from a little while back...
    http://forums.devshed.com/c-programm...ry-314721.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  3. Creating a file in a certain directory
    By bc17 in forum C++ Programming
    Replies: 10
    Last Post: 11-24-2002, 12:20 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21