Click here to Skip to main content
15,889,736 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello ..

Am retriving the application Stratup path like this:

C#
char buffer2[2048];
char buffer1[5120];
GetModuleFileName(NULL, buffer2, 5120);


In buffer2 am getting output as:
c:\Users\guruprasad\Desktop\gscanner\Debug\gscanner.exe

And,Actually i want to truncate gscanner.exe, so that my final output should be:
c:\Users\guruprasad\Desktop\gscanner\Debug\

Please suggest me..


Thanks in Advance..!
Posted
Comments
[no name] 4-Apr-12 1:47am    
Is the question asking how to remove "gscanner.exe" from the string?
Sergey Alexandrovich Kryukov 4-Apr-12 1:48am    
Apparently... the usual reply in such cases: "What have you done so far?".
--SA
Guru_C++ 4-Apr-12 1:49am    
Yes.. I want to remove or truncate the string "gscanner.exe"..
[no name] 4-Apr-12 1:54am    
What have you tried?
Guru_C++ 4-Apr-12 2:02am    
char* temp=buffer2;
int buff_len=strlen(temp);
for(int i=buff_len;i>0;i--)
{
temp[i]='\0';
if(i==43)
break;
}

With this code, if i rename the "gscanner.exe" to some other name.. Then it wont work..

There is API GetCurrentDirectory function[^]
And this will get you application startup directory:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

#define BUFSIZE MAX_PATH

void _tmain()
{
   TCHAR Buffer[BUFSIZE];
   DWORD dwRet;

   dwRet = GetCurrentDirectory(BUFSIZE, Buffer);
   _tprintf(TEXT("Current directory: %s\n"), Buffer);

}
 
Share this answer
 
v2
So what you want to do is:

- start at the end of the string and search backwards for the first backslash ('\') character

- if found, cut the string off exactly after that backslash

- if not found, do nothing

I don't give you the solution right away, but let you give a try at it. It is not really difficult and there is even a library routine for searching a string backwards for a specific character. You might go looking for it.

If you are not able to find the solution, which I doubt, I will give you some more hints.
 
Share this answer
 
v2
C++
std::string ExtractDirectory( const std::string& path )
  {
  return path.substr( 0, path.find_last_of( '\\' ) +1 );
  }

int main()
{
char buffer2[2048];
char buffer1[5120];


GetModuleFileName(NULL, buffer2, 2048);

std::string dir = ExtractDirectory(std::string(buffer2));

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900