Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Adding a string to the end of a pointer using strcat()

I am currently trying to create a program as part of my Degree programme.

I am required to create a program to mimic the "ls -alR" command.

I can read in the directory and display it correctly, but controlling the directory currently being in is done through a string which is passed as a parameter to a function, ie  recurse(char *currpath)

I am then within the code trying to add a "/" to the end using strcat(currpath, "/");

Unfortunately, here is where I am getting a 'Segmentation fault'.

Any help with this owuld be appreciated.

-Tefal
0
Tefal
Asked:
Tefal
1 Solution
 
akshayxxCommented:
ur strcat is giving segfault because it doesnt have enough of space to hold more characters..
do this

say
char *str;
str=(char*)malloc(strlen("/home")+1);
strcpy(str,"/home");

so str is having "/home"
to add '/' do like this......
str=(char*)realloc(str,strlen(str)+2);
sprintf(str,"%s%c",str,'/');

printf("%s\n",str);
0
 
ExceterCommented:
>> Unfortunately, here is where I am getting a 'Segmentation fault'.

This means that strcat is overstepping the array's bounds.

>> strcat(currpath, "/");

Try the following instead,

char* cpath = (char*)malloc( sizeof(char) * strlen(currpath) + 2 );

sprintf( cpath, "%s%s", currpath, "/" );

Exceter
0
 
nrohanCommented:
who has allocated memory to currpath and how much?
problem seems here that, when you append "/" to currpath, it overwrites some other memory.
try allocating 1/2 extra bytes to currpath.

if u post some code that will help.

rohan
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ExceterCommented:
Sorry, akshayxx. You posted while I ws typing. :-)

Exceter
0
 
akshayxxCommented:
btw if u need to just list the directory .. then u might want to have a look on this
http://nodevice.com/sections/ManIndex/man1383.html
look at the example given in the manual page
0
 
akshayxxCommented:
for Exceter:
         :)
0
 
KocilCommented:
What does the recurse(char *currpath) do ?
Does it call itself again  recursivelly ?
Then maybe your stack is not enough.

Bad guess :)
Complete code will be easier to shoot.
0
 
akshayxxCommented:
Exceter :
our posts were pretty close ... i wud like to see the time difference to the last nano-second :P
0
 
ExceterCommented:
>> our posts were pretty close ... i wud like to see the time difference to the last nano-second :P

Yeah, that would be pretty interesting. :-)

Exceter
0
 
plusheyCommented:
Isn't sprintf a little OTT?

If I were to use sprintf then why not sprintf(cpath, "%s/", currpath);
however I much prefer
strcpy(cpath, currpath);
strcat(cpath, "/");
Why? because I don't like using sledgehammers to crack nuts :-)

Also if this function is recursive then you really don't want to be using malloc etc... without a call to free before returning.
0
 
akshayxxCommented:
plushey,
yes u r true ..actually  we(me and ex)  were racing to post the answer fast , so never checked for shortcuts.. :),
good for me that exceter didnt get this , that cud have saved seconds to him.
0
 
ExceterCommented:
>> good for me that exceter didnt get this , that cud have saved seconds to him.

:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now