Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

How can i create a file in sub current working directory

Hi,

I would like to create an file in sub directory of currect working directory.
i am using this to create file.
char path_buffer[_MAX_PATH];
ofstream lrn_file(path_buffer);

i would like to fill the "path_buffer" variable with the path with  sub directory of currect working directory.
(Ex. c:\kc  is current directory , i have sub directory with c:\kc\kc)

Thanks
0
vihar123
Asked:
vihar123
  • 2
  • 2
2 Solutions
 
AlexFMCommented:
You need to use relative path.

"kc\\file.txt"
0
 
vihar123Author Commented:
Thank you, I am trying like this.

char buf[_MAX_PATH];
GetCurrentDirectory(_MAX_PATH,buf);

I have the current directory path.(c:\kc)

I would like to create file in sub folder of "kc" ( c:\kc\kc)

How can i proceed from here.

Thanks

0
 
AshokCommented:
strcpy(path_buffer, "/kc");
ofstream lrn_file(path_buffer);

HTH
0
 
AlexFMCommented:
I don't know what function do you want to use for creating file - stream class, CreateFile etc. But you don't need to use GetCurrentDirectory. All you need is to use relative path. For example, if you want to create file test.txt in kc subdirectory, you need to write:

char buf[_MAX_PATH];
strcpy(buf, "kc\\test.txt");

Now you can use buf as parameter for any function which creates file.

If you want to use GetCurrentDirectory:

char buf[_MAX_PATH];
GetCurrentDirectory(_MAX_PATH,buf);
strcat(buf, "\\kc\\test.txt");

Result will be the same.
0
 
AshokCommented:
I think it should be...

strcat(path_buffer, "/kc");
ofstream lrn_file(path_buffer);

HTH
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now