extract characters to '.'

This is a discussion on extract characters to '.' within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to write a function to extract a number of characters in a file name given to ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    extract characters to '.'

    Hello,

    I am trying to write a function to extract a number of characters in a file name given to a function up to the '.' character.

    My function is below. I am trying to copy the characters pointed to by the fName pointer to a data member dataSetName up to the '.' operator reached in the fName pointer.

    Code:
    void Layer::setDataSetName(char * fName)
    {
    	int result;	//result buffer
    
    	//if it is a featureclass
    	if(result = (strcmp(layerType, "featureclass") == 0))
    	{
    		int count = 0;	//holds count of characters in argument
    		
    		//count num of characters up to the '.' character
    		for(int i = 0; fName[i] != '.'; i++)
    		{	
    			++count; 
    		}
    
    		cout << "count is " << count << endl;
    
    		//allocate space for the dataSetName
    		dataSetName = new char[count + 1];
    		
    		//copy the characters up the '.' character
    		for(; *fName != '.'; fName++, dataSetName++)
    		{ 
    			*dataSetName = *fName;
    			cout << "*dataSetName = " << *dataSetName << " *fName = " << *fName << endl;		
    		}
    
    		//place a null character after the last character
    		*dataSetName = '\0';
    
    	}	//end if a featureclass
    
    	//else it is an image
    	else
    	{
    		//keep the extension
    		dataSetName = new char[strlen(fName) + 1];
    		strcpy(dataSetName, fName);
    
    	}	//end else it is an image
    	
    }	//end setDataSetName
    For some reason I get no output for the dataSetName. Why is this not working?
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Maybe try replacing (strcmp(layerType, "featureclass") == 0) with !(strcmp(layerType, "featureclass") == 0). I didn't look at the rest of your code, but .. err... whatever.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    That part is ok. I should have mentioned the code will enter the if statement in this case.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    > dataSetName = new char[count + 1];
    It's pointing to the start of the allocated memory,

    But by the time you do this
    > *dataSetName = '\0';
    It's pointing to the end of your allocated memory

    Use strncpy to save all this messing about copying a char at a time

  5. #5
    Unregistered
    Guest
    if(result = (strcmp(layerType, "featureclass") == 0))

    this looks like an assignment which is a no no in conditional statements.

    with strcpy you will copy the . into the new buffer, which he doesn't want to do. Therefore char by char copying is fine, it's actually what strcpy() does anyway. However, I would copy it using count and indexes for clarity sake.

    Code:
    for(int i = 0; i < count; i++)
      dataSetName[i] = fname[i];
    
    dataSetName[i] = '\0';

  6. #6
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    Try using strtok to extract the filename.
    Code:
    #include <string.h>
    
    ...
    
    char *p;
    p = strtok(filename, ".");
    cout << "filename : " << p << endl;
    p = strtok(NULL, ".");
    cout << "extension : " << p << endl;
    
    ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 07:41 PM
  3. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 07:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21