C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-03-2006, 09:30 AM   #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.
1978Corvette is offline   Reply With Quote
Old 03-03-2006, 10:23 AM   #2
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
Which OS/Compiler are you using?
Salem is offline   Reply With Quote
Old 03-03-2006, 10:55 AM   #3
Registered User
 
Join Date: Mar 2005
Posts: 15
Win XP/ Dev-C++
1978Corvette is offline   Reply With Quote
Old 03-03-2006, 11:45 AM   #4
Been here, done that.
 
Join Date: May 2003
Posts: 1,036
Yes, fopen() for write will create the file.
__________________
There are only 10 types of people in the world -- those that use binary, and those that don't
WaltP is offline   Reply With Quote
Old 03-03-2006, 11:57 AM   #5
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
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");
bithub is offline   Reply With Quote
Old 03-03-2006, 02:05 PM   #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...
tcpl is offline   Reply With Quote
Old 03-03-2006, 04:44 PM   #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?
1978Corvette is offline   Reply With Quote
Old 03-03-2006, 04:50 PM   #8
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Quote:
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.

Quote:
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).
bithub is offline   Reply With Quote
Old 03-03-2006, 10:01 PM   #9
Registered User
 
Join Date: Feb 2006
Posts: 17
Quote:
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...
tcpl is offline   Reply With Quote
Old 03-03-2006, 10:11 PM   #10
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Posts: 4,990
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.*
Dave_Sinkula is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ std routines siavoshkc C++ Programming 33 07-28-2006 12:13 AM
Batch file programming year2038bug Tech Board 10 09-05-2005 03:30 PM
Creating a file in a certain directory bc17 C++ Programming 10 11-24-2002 12:20 PM
simulate Grep command in Unix using C laxmi C Programming 6 05-10-2002 04:10 PM
My program, anyhelp @licomb C Programming 14 08-14-2001 10:04 PM


All times are GMT -6. The time now is 01:29 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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