Ok, so I'm working on this assignment which is supposed to somewhat imitate Unix's cp command (with files "foo.txt" and "bar.txt"). It's been going good up until this section:
Code:
char* getPath(char* path, int level)
{ char** dirList = breakdown(path);
if(a==1){ //if path string was only the filename, prepend cwd path
char tmp[strlen(path)];
strcpy(tmp,path);
char* newPath = getcwd(buf,128);
strcat(newPath, "/");
strcat(newPath, path);
return newPath;
}
path = "";
for(int i=0;i<a-level;i++){
strcat(path, dirList[i]);
strcat(path, "/");
}
return path;
}
void copy(char* srcFile, char* destFile)
{ FILE *destination, *source;
srcFile = getPath(srcFile,0);
destFile = getPath(destFile,0);
if(fopen(destFile,"r")!=NULL){
printf("%s already exists in this directory.\nOverwrite?"
"\n",getName(destFile));
if(getchar()=='n')
return;
}
destination = fopen(destFile, "wb");
source = fopen(srcFile, "rb");
fWrite(source,destination);
}
So when debugging with GDB, I step past srcFile = getPath(srcFile,0); and read the value of srcFile to be "/cygdrive/c/Users/.../foo.txt" as expected, but when I step past the next line (destFile = ...), the value of srcFile changes to "/cygdrive/c/Users/.../foS". I've tried allocating memory for the strings, but problem persists.
Can anyone see why this is happening?