The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-26-2008
Registered User
 

Join Date: Nov 2007
Posts: 72
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]
}
For folder[] (which is the folder whose name I want) I will get something like "myfolder\023\04" when I just want "myfolder".

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.
Reply With Quote
Forum Sponsor
  #2  
Old 06-26-2008
Registered User
 

Join Date: Nov 2007
Posts: 72
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 ?
Reply With Quote
  #3  
Old 06-27-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
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.
Reply With Quote
  #4  
Old 06-27-2008
Registered User
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 250
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
Reply With Quote
  #5  
Old 06-29-2008
Registered User
 

Join Date: Nov 2007
Posts: 72
Quote:
Originally Posted by shamrock View Post
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.
Sorry str[i] is the variable in question and I declared it as a pointer.

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.
Reply With Quote
  #6  
Old 07-02-2008
Registered User
 

Join Date: Dec 2007
Location: Simi Valley, CA
Posts: 28
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;
}
Reply With Quote
  #7  
Old 07-18-2008
Registered User
 

Join Date: Apr 2006
Location: Northern Virginia
Posts: 23
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:03 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0