![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to replace any char with newline char. | mightysam | Shell Programming and Scripting | 5 | 09-18-2008 05:15 PM |
| Carreer:Networking Programming in Unix (C programming Language) | vibhory2j | UNIX for Dummies Questions & Answers | 5 | 09-05-2008 04:57 PM |
| Problem with arrays in awk | Glauco | Shell Programming and Scripting | 5 | 05-12-2008 01:43 PM |
| char *p and char p[]. | arunviswanath | High Level Programming | 4 | 07-19-2006 11:11 PM |
| char array problem | DebianJ | High Level Programming | 4 | 06-25-2005 07:41 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
C programming + problem with char arrays
Im trying to write some code atm which gets the complete pathname of a folder and strips off references to the parent folders. The end result should be just the name of the folder.
Currently Im able to extract the folder name, however Im getting junk added onto the name as well which is making further processing more difficult. This is how Im doing it Code:
/*Count number of parent folders*/
for(i=0;str[i];i++)
{
if((int)str[i] == (int) '/')
slash_cnt++;
}
/*Extract name of folder*/
for(i=0;str[i];i++)
{
if((int)str[i] == (int) '/')
{
cnt++;
if(cnt == slash_cnt)
{
flag=1;
pos = i;
}
}
if( (flag==1) && (i>pos) && (i<strlen(str)) )
folder[i-pos-1] = str[i]
}
Im not sure what I could be doing wrong, so help would be appreciated cheers Last edited by JamesGoh; 06-26-2008 at 09:40 PM. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Gday all
Ive managed to get it to work now, however this involved replacing the char * with char[] (I was using char pointers beforehand to contain the full path of the folder). Also Im aware you can reference pointers as if they were an array, so could the problem I was getting in the firstplace be related to the way I was referencing ? |
|
#3
|
|||
|
|||
|
The posted code snippet doesn't declare whether you are using char * or char [] for the variable in question and which one of 'em is that variable. Another approach is to find the end of the complete pathname and then walk backwards stopping at the first slash that comes across. Name of last folder starts one character to the right of the slash.
|
|
#4
|
|||
|
|||
|
Did you take a look at the source of basename?
DragonflyBSD has pretty respectable code to browse via CVSweb. src/usr.bin/basename/basename.c - view - 1.11 |
|
#5
|
|||
|
|||
|
Quote:
The declaration is as follows Code:
char *str; /*char array of 1000 bytes ( 1 kb)*/ char cmd[1000]; str = (char *)malloc(sizeof(cmd)); I referenced *str as an array to simplify access to it's value, so sorry for any confusion. |
|
#6
|
|||
|
|||
|
pathnames
I didn't spend much time testing this, but I think it'll work fine.
#include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) { string separator="/"; // use standard path separator unless backwards slashes are discovered if (argc!=2) {cout<<"USAGE: pathname pathname"<<endl<<"RETURNS: foldername, no parent."<<endl; return -1;} string pathname; pathname=argv[1]; cout<<"input:"<<pathname<<endl; size_t lastslash=pathname.find_last_of("/"); size_t lastbslash=pathname.find_last_of("\\"); if ((lastslash!=string::npos)&&(lastbslash!=string::npos)) {cout<<"use either \\ or /, but not both"<<endl; return -1;} if (lastslash==string::npos) {lastslash=lastbslash;separator="\\";} if (lastslash==string::npos) {cout<<pathname<<endl; return 0;} if ((lastslash+1)>=pathname.size()) {cout<<"error"<<endl; return -1;} size_t parentslash=pathname.find_last_of(separator.c_str(),lastslash-1); cout<<"output..."<<endl; if (parentslash==string::npos) {cout<<&pathname[lastslash+1]<<endl; return 0;} if (parentslash==lastslash) {cout<<pathname<<endl; return 0;} parentslash++; cout.write(&pathname[parentslash],lastslash-parentslash); cout<<endl; return 0; } |
|
#7
|
|||
|
|||
|
Just a suggestion, but you might want to look at using the strtok() subroutine. It will provide for you that which is separated by your "/", your directory names, and save lots of iterative coding. Tastes great and much less filling.
|
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|