Question

I have to make a function in C that can swap a file extension into another. For instance:

swapExtension("myFile.c", "o");

and the prototype is:

char * swapExtension(char * fileName, char * fileExtension).

I am not getting anything but a segfault. My code is:

{
    char * temp = strchr(fileName, '.');
    printf("%s",temp);
    strncpy(temp, nouvelleExtension, 2);
}
Was it helpful?

Solution

There are two problems in your code 1) You are using the char * are are trying to modify that protected memory inside your code

2) You are trying to return a local address from your function which is also not good.

3) What if the filename contains a DOT '.' in its filename.

You can use char array as a solution but still the second problem remains, for that you should allocate some memory and then copy the returning string into that memory

char* swapExtension(char * fileName, char * fileExtension)
{
int newext = strlen(fileExtension);
bool flag =false;
int size = strlen(fileName);
int ext = 0;
for(int i=size-1;i>=0;i--)
{
    if(fileName[i] == '.')
    {
        flag = true;
        ext = (size -1) - i;
        break;
    }
}
if(!flag){
    printf("no extension found");
    return NULL;
}
int retsize = size - ext + newext + 1;
char * retBuff = new char[retsize];
retBuff[0]='\0';
memcpy(retBuff,fileName,retsize);
strncpy(&retBuff[size-ext], fileExtension, newext);
return retBuff;

}

OTHER TIPS

As Baldrick mentioned, you're passing a (pointer to) string constant to swapExtension, you then pass that string constant to strchr which in turn returns a part of that same string constant. Then with strncpy you write to the string constant which is undefined behaviour and in your case segfaults.

Use a string (character) array instead:

char myFile[] = "myFile.c";
swapExtension(myFile, "o");

Now temp in swapExtension will write to the myFile array which is valid. Though, be aware that this will only work when the length of the extensions is the same, otherwise there won't be enough space in the array. To prevent this, use:

char myFile[50] = "myFile.c";

The array will initially contain the same string but will also have space for a longer one.

See also: What is the difference between char s[] and char * s in C

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top