how to write output from a c++ program to a subdirectory of a directory where the .exe resides.
-
Hi I would like to tidy up the directories. So I like to my program to read input from a sub-directory and write output to a sub-directory. All these two sub-directories are under the directory where the .exe resides. FILE *f_ptr1; CString ftitle; f_ptr22[i] = fopen("rs_sf"+ftitle,"w"); fprintf (f_ptr2, "%.6lf\n",v[2*i]); Thanks
-
Hi I would like to tidy up the directories. So I like to my program to read input from a sub-directory and write output to a sub-directory. All these two sub-directories are under the directory where the .exe resides. FILE *f_ptr1; CString ftitle; f_ptr22[i] = fopen("rs_sf"+ftitle,"w"); fprintf (f_ptr2, "%.6lf\n",v[2*i]); Thanks
your "code" does not make sense. What's the question ? - You know how to create files/directories and write to them ? - You know where the executable resides ? - You know how to check if you have permission to write there ? As far as I know, it's bad practice to write files where the exe resides, more than often a user will not be able to do so, use the user's "document" folders. Good luck. Max.
Watched code never compiles.
-
Hi I would like to tidy up the directories. So I like to my program to read input from a sub-directory and write output to a sub-directory. All these two sub-directories are under the directory where the .exe resides. FILE *f_ptr1; CString ftitle; f_ptr22[i] = fopen("rs_sf"+ftitle,"w"); fprintf (f_ptr2, "%.6lf\n",v[2*i]); Thanks
mrby123 wrote:
f_ptr22[i] = fopen("rs_sf"+ftitle,"w");
If you really have to, try:
f_ptr22[i] = fopen(".\\subdir\\rs_sf" + ftitle, "w");
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Hi I would like to tidy up the directories. So I like to my program to read input from a sub-directory and write output to a sub-directory. All these two sub-directories are under the directory where the .exe resides. FILE *f_ptr1; CString ftitle; f_ptr22[i] = fopen("rs_sf"+ftitle,"w"); fprintf (f_ptr2, "%.6lf\n",v[2*i]); Thanks
As Maximilien said, programs are normally installed under the
C:\Program Files
directory. You need administrative privileges to create folders and files under this directory. You can get the Documents folder using theSHGetSpecialFolderLocation
API and specifyingCSIDL_MYDOCUMENTS
in itsnFolder
parameter.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
mrby123 wrote:
f_ptr22[i] = fopen("rs_sf"+ftitle,"w");
If you really have to, try:
f_ptr22[i] = fopen(".\\subdir\\rs_sf" + ftitle, "w");
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Thanks, I tried. It does not work. I need: if the sub directory does not exist,create it and write the file in it, if it exists, write the file in it without re-create it. Thanks for further suggestions.
Well, what I showed would obviously require that the "subdir" folder exist. Use
CreateDirectory()
first."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Well, what I showed would obviously require that the "subdir" folder exist. Use
CreateDirectory()
first."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Thanks. I think I also need a test statement to check if the directory exists. If it does not exist, then create the directory. Otherwise, just write the file in the directory. What is the statement for checking if a directory exist ? Thanks again
mrby123 wrote:
What is the statement for checking if a directory exist ?
You could either call
_access()
or letCreateDirectory()
do it for you."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius