C++ program to print the path without the file name!
-
here it is the C code , it prints the base filename I want to modify it to print the following /work1/data/xxxx/yyy/ const char * path = "/work1/data/xxxx/yyy/file_name.txt"; const char * filename; filename = path+strlen(path); while (filename != path && *(filename) != '/') filename--; if ( filename != path) filename++; cout << filename;
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] -
traverse the char array from end (in reverse way) till you see a / and then replace '\0' there. did this not work for you? i found, you asked the same question on 8th july see the answers here http://www.codeproject.com/Messages/3110995/String-Parsing-in-Cplusplus-Simple-Question.aspx[^]
-------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.
-
You can use PathRemoveFileSpec()[^] to strip out the filename. Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
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] -
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
-
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
-
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
-
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
-
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 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] -
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
-
this logic worked for me perfectly.
const char \* path = "/work1/data/xxxx/yyy/file\_name.txt"; char path1\[256\]; strcpy(path1,path);//copying the char array cout << path <<"\\n";//this prints the whole filename with path. int l=strlen(path);//finding the length of the path for(int i=l;i>0;i--)//reverse traversing the array { if(path1\[i\]=='/')//if '/' is found, then replacing it with '\\0'. { path1\[i\]='\\0'; break; } } cout << path1<<"\\n";//this prints only the path.
the output is as you desired.
-------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.
-
the code i posted prints the base file name not the path I want to print the path without file name
see here http://www.codeproject.com/Messages/3118524/Re-Cplusplus-program-to-print-the-path-without-the-file-name.aspx[^] and also try with the David crow's recent post that would be easier than mine.
-------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.
-
this logic worked for me perfectly.
const char \* path = "/work1/data/xxxx/yyy/file\_name.txt"; char path1\[256\]; strcpy(path1,path);//copying the char array cout << path <<"\\n";//this prints the whole filename with path. int l=strlen(path);//finding the length of the path for(int i=l;i>0;i--)//reverse traversing the array { if(path1\[i\]=='/')//if '/' is found, then replacing it with '\\0'. { path1\[i\]='\\0'; break; } } cout << path1<<"\\n";//this prints only the path.
the output is as you desired.
-------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.
-
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){/*handle error*/}filename = basename+strlen(basename);
while (filename != basename && *(filename) != '/')
filename--;if ( filename != basename)
{
*(filename+1)='\0';
}
else
*basename='\0';printf("%s\n", basename);
free(basename); /* REMEMBER TO FREE */:)
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] -
this logic worked for me perfectly.
const char \* path = "/work1/data/xxxx/yyy/file\_name.txt"; char path1\[256\]; strcpy(path1,path);//copying the char array cout << path <<"\\n";//this prints the whole filename with path. int l=strlen(path);//finding the length of the path for(int i=l;i>0;i--)//reverse traversing the array { if(path1\[i\]=='/')//if '/' is found, then replacing it with '\\0'. { path1\[i\]='\\0'; break; } } cout << path1<<"\\n";//this prints only the path.
the output is as you desired.
-------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.