I'm writing a function that removes the last segment of the path to a file, but I'm not getting the right string back from my function. I give the function the input sample/four.txt/one.txt" as a sample parameter, and I want to get sample/four.txt back. I've tried the code on my own machine, and getOldPath always returns sample/f. Inside the getOldPath function, curPath is sample/four.txt. Once I exit and assign it to newPath in the main, newPath is sample/f. However, when I run the program on my friend's machine, he gets the expected sample/four.txt. What is causing this issue and how can I fix it? Here is the code that I am working with:
#include <stdio.h>
#include <string.h>
char *getOldPath(char *curPath)
{
// Loop to find the count of characters
int count;
count = 0;
int end;
end = strlen(curPath);
char tempChar;
tempChar = curPath[end-1];
while(tempChar != '/')
{
count++;
end--;
printf("End is: %i\n",end);
tempChar = curPath[end];
}
char temp[256];
int numChar;
numChar = strlen(curPath) - count;
strncpy(temp,curPath,numChar);
curPath = temp;
printf("The path is: %s\n",curPath);
return curPath;
}
int main(int argc, char **argv)
{
char *path = "sample/four.txt/one.txt";
char *newPath = getOldPath(path);
printf("Do we get the new path back: %s\n",newPath);
}