Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing files to a different directory

Status
Not open for further replies.

scimatic

Technical User
Aug 30, 2004
19
US
How do I create a user specified folder and then have all my ouput files be written to that folder?

I'm currently using this to create the folder:

system("mkdir Records");

But I cannot get the files generated from my program to be written to that folder.
 
CreateDirectory can create a folder. Use an ofstream to write files to that folder. Actually, I believe that ofstream will create the folder if it does not already exist, so just specify the path to the directory you want when you call ofstream's open method and it should be fine.
 
No any directories in C/C++ file stream models. Both models manipulate with files only. These platform independent languages implemented on lots of systems where no directory notion at all.
Class ofstream can't create a new directory.
So any directory mechanics is a platform-specific part of the language library.
See mkdir() non-standard library function (<direct.h> header in MS implementation). This function implemented on Unix too (or use CreateDirectory() Win API as timmay3141 said)...
 
I've been having the same problem.

I'm writing some c++ code which creates a subdirectory and then renames files from the parent into the newly created directory.

I'm using mkdir() to create the directory, then, immediatley after, using the rename() function:

mkdir("newdir");
rename( "c:\hello.txt", "c:\newdir\newhello.txt" )



The rename function keeps failing.
(renaming files into a preexisting directory works fine)

Another puzzling thing, is that if I run vc++ in debug mode and pause it momentarily just after the directory is created, the renaming then works fine!

Any ideas on how to overcome the problem?
 
> rename( "c:\hello.txt", "c:\newdir\newhello.txt" )
If these strings appear in your source code, then you need to escape those back-slashes.

[tt]rename( "c:\\hello.txt", "c:\\newdir\\newhello.txt" );[/tt]

In any event, you should be checking the return result
Code:
int status = rename( "c:\\hello.txt", "c:\\newdir\\newhello.txt" );
if ( status == -1 ) {
  perror("rename failed");
}

--
 
I found that I can use: system("mkdir directory") to create the folder and then change the working directory by using:

#include <direct.h>

_chdir("directory"); //change working directory

... write files ...

_chdir(".."); //go back to main directory
 
I like the rename method, however I want my program to run from the CDROM drive. How do I get the path to where the program is running. Particularry the drive letter.

Thanks
 
Use _splitpath() from <cstdlib> (it's not a C/C++ standard function):
Code:
	char	drv[_MAX_DRIVE];
	char	dir[_MAX_DIR];
	char	name[_MAX_FNAME];
	char	ext[_MAX_EXT];

	drv[0] = dir[0] = name[0] = ext[0] = 0;
	_splitpath(path,drv,dir,name,ext);
Use argv[0] in main: it's your exe full path. Split it if you need.
Use _makepath() to construct new paths, for example:
Code:
...
char cwd[_MAX_PATH];
char drv[_MAX_DRIVE];
char dir[_MAX_DIR];
_getcwd(cwd,sizeof cwd);
_splitpath(cwd,drv,dir,0,0);
char newdir[_MAX_PATH];
_makepath(newdir,drv,dir,"subdirname",0);
if (_mkdir(newdir))
{
// Can't create...
}
And so on...
Apropos, Windows can't move directories via rename().
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top