LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-08-2005, 05:14 AM   #1
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Rep: Reputation: 0
Why are some elements missing after using strcat()


Hello,

I am trying to split the unix folder path from the file name that is entered.

For example, if the path entered is "/home/user/temp/asdf.txt"

I want to return 2 things...
Folder name: "/home/user/temp"
File Name: "asdf.txt"
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define WORD_COUNT 10

int main (int argc, char *argv[] )
{
  char text[] = "/The/quick/brown/fox/jumped/over/the/lazy/red/dog";
  char *wlist[WORD_COUNT]; /*array of 10 pointers to characters*/
  char *nwlist[WORD_COUNT];
  int i;


  printf("text[%s]\n", text);


  wlist[0] = strtok( text, "/" );
  nwlist[0] = strtok( text, "/" );


  for ( i=1; i < WORD_COUNT; i++ )
{
    wlist[i] = strtok( NULL, "/" );
}

  for ( i=0; i < WORD_COUNT-1; i++ )
{
    strcat(wlist[i], "/");
    printf("\n%s",nwlist[i]);
}

  return 0;
}
On running this, I get the following output...
Code:
text[/The/quick/brown/fox/jumped/over/the/lazy/red/dog]

The/
/
brown/
/
jumped/
/
the/
/
I have used " \n" to ensure readibility...

Kindly help me if i am wrong anywhere...

Thank you.

-Monil
 
Old 03-08-2005, 05:51 AM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Not a solution to your question, but why not to search the filename from the end of file? Short for loop searching the characters will be faster and probably shorter.
 
Old 03-08-2005, 06:04 AM   #3
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Original Poster
Rep: Reputation: 0
Right, But, I also need the directory path too...

then, how do i remove the path and the file name?

Any idea?

-Monil
 
Old 03-08-2005, 07:36 AM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
it's happening because strtok() modifies the string; it doesn't allocate new ones. So wlist is an array of pointers into the string text, which has its /'s turned into '\0's. When you strcat() onto each of those pointers, it writes a '\0' into the first character of the string pointed to by the next pointer. (Actually, this is only true for every other pointer, since for half of them strcat sees a 0 length first argument and overwrites a character that is not in any existing string anymore.)

That's not very clear as it's way too early in the morning. Basically you just need to realize what strtok is doing to your original string.
 
Old 03-08-2005, 07:40 AM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
also, see dirname(3)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
strcat Core Dump monil Programming 4 03-08-2005 11:20 AM
best way to avoid buffer overflow... with strcpy and strcat os2 Programming 4 03-02-2005 02:24 PM
strcat() error with gcc sureshkellemane Programming 17 02-21-2005 12:42 AM
Segmentation fault on strcat() Ephracis Programming 8 12-17-2004 01:28 AM
RH 8.0 strcat problem shibdas Programming 1 07-02-2003 01:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration