|
it didn't work
I need to print the path without the baseile name
|
|
|
|
|
this logic worked for me perfectly.
const char * path = "/work1/data/xxxx/yyy/file_name.txt";
char path1[256];
strcpy(path1,path);
cout << path <<"\n";
int l=strlen(path);
for(int i=l;i>0;i--)
{
if(path1[i]=='/')
{
path1[i]='\0';
break;
}
}
cout << path1<<"\n";
the output is as you desired.
--------------------------------------------
Suggestion to the members:
Please prefix your main thread subject with [SOLVED] if it is solved.
thanks.
chandu.
|
|
|
|
|
sorry,
it didn't work
why?
look at the way you are using strcpy() function
|
|
|
|
|
ksaw123 wrote: look at the way you are using strcpy() function
whats the problem?
--------------------------------------------
Suggestion to the members:
Please prefix your main thread subject with [SOLVED] if it is solved.
thanks.
chandu.
|
|
|
|
|
You don't need to parse. Use SHString apis.
|
|
|
|
|
|
it is C/C++ program not vasuil C++
|
|
|
|
|
Use PathRemoveFileSpec() or _splitpath() .
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
Which .h file those functions is in?
|
|
|
|
|
See here.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
my boss
I don't want to use Microsoft C++ , I am using the lagacy C/C++
actually , my code is working but i wanna print the path without the file name as your suggested function.
Thanks
|
|
|
|
|
You might want to consider strrchr() to find the last backslash. Put a '\0' character there.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
And what is the question?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
How can I print the path without the base filename
|
|
|
|
|
The code you posted does it.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
the code i posted prints the base file name not the path
I want to print the path without file name
|
|
|
|
|
|
you're right:
const char * path ="/work1/data/xxxx/yyy/file_name.txt";
char * basename;
char * filename;
basename = _strdup(path);
if ( !basename){}
filename = basename+strlen(basename);
while (filename != basename && *(filename) != '/')
filename--;
if ( filename != basename)
{
*(filename+1)='\0';
}
else
*basename='\0';
printf("%s\n", basename);
free(basename);
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hi
I have a class like:
class CMyclass1
{
public
CMyclass2 myclass;
.....
}
class CMyclass2
{
public
TCHAR Var[SOMEVALUE];
.....
}
CMyclass2 ::CMyclass2
{
memset(Var, '\0',sizeof(Var));
strcpy_s(Var,"some value");
}
----------- When I did following, program crashed ---
CMyclass1 * pclass = new CMyclass1()
delete pclass;
How can fix this problem?
Best regards,
|
|
|
|
|
There are 2 reasons:-
1. If you give SOMEVALUE=3 and copy "some value" it will fail and program will crash, so please check that.
2. what is need of memset when you are allocating in a stack?, Doing this also fails.
So handle heap allocation carefully.
|
|
|
|
|
Hi
I did following:
#define SOMEVALUE 100
Why this will cause problem?
I used "memset" to initialize the array. What else can I use to initialize an array?
So "memset" will use HEAP? I deleted the "memset", the bug is still there.
Best regards,
modified on Monday, July 13, 2009 9:51 AM
|
|
|
|
|
Use the debugger to step through the code to find the offending line. Aside from some syntax errors, your code works fine with VS6.
Using memset() is fine, although unnecessary in the context in which you are using it.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
I did following to solve the problem.
class CMyclass2
{
public
TCHAR* Var;
.....
}
CMyclass2::CMyclass2
{
Var = new TCHAR[SOMEVALUE];
}
VC++ does not allow TCHAR array in a class definition? Actually, TCHAR array is a pointer.
|
|
|
|
|
transoft wrote: VC++ does not allow TCHAR array in a class definition?
Sure it does.
transoft wrote: Actually, TCHAR array is a pointer.
It is now, but it wasn't earlier.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
Hi
Why it crashes when I use TCHAR array in the class definition and it works just fine when I use TCHAR pointer?
Thanks,
|
|
|
|