?
Solved

How can i create a file in sub current working directory

Posted on 2005-03-02
5
Medium Priority
?
349 Views
Last Modified: 2008-03-04
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
Comment
Question by:vihar123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 13438610
You need to use relative path.

"kc\\file.txt"
0
 

Author Comment

by:vihar123
ID: 13438649
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
 
LVL 13

Accepted Solution

by:
Ashok earned 300 total points
ID: 13438671
strcpy(path_buffer, "/kc");
ofstream lrn_file(path_buffer);

HTH
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 300 total points
ID: 13438695
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
 
LVL 13

Expert Comment

by:Ashok
ID: 13438708
I think it should be...

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

HTH
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question