0

I am trying to create a function that extract the extension from a file name. file_name points to a string containing a file name. The function should store the extension on the file name in the string pointed to by extension. For example, if the file name is “memo.txt”, the function will store “txt” in the string pointed to by extension. If the file name doesn’t have an extension, the function should store an empty string (a single null character) in the string pointed to by extension.

This is what I have:

  #include <stdio.h>

void get_extension(char *file_name, char *extension);
int main(){
    char *ex;
    get_extension("hello.txt", ex);

    char *p;
    for(p = ex; *p != '\0'; p++){
        printf("extension: %c", *p);
    }

    return 0;
}

void get_extension(char *file_name, char *extension){

    char *p;

    for (p = file_name; *p != '\0'; p++){

        if(*p == '.'){
            p++;

            while(*p != '\0'){
                *extension = *p;
                p++;
                extension++;
            }
        }  *extension = '\0';
    }

}

I keep getting a segmentation fault error and I don't know what is wrong. Can someone help me, please? Thanks!

Mark Santos
  • 108
  • 11
  • 4
    You have a pointer `ex`, but *where does it point?* When you do `*extension = *p`, where are you writing? – Some programmer dude Mar 07 '20 at 02:21
  • Does this answer your question? [Getting file extension in C](https://stackoverflow.com/questions/5309471/getting-file-extension-in-c) – lenik Mar 07 '20 at 03:40

2 Answers2

1

You should change this part:

int main(){
    char *ex;

into something that not only declares a pointer, but allocates the memory to store the result:

int main(){
    char ex[100];

Besides I'd recommend you to take a look at strrchr() that can help you to find the extension much easier: Getting file extension in C


Also I would like to notice that this code:

char *p;
for(p = ex; *p != '\0'; p++){
    printf("extension: %c", *p);
}

will not work as you think it might. For example for the "hello.txt" is will print:

extension: textension: xextension: t

Not sure if this is what you want.

lenik
  • 23,228
  • 4
  • 34
  • 43
1

There can be 2 reasons for your seg fault.

1) ex doesn't point to any memory. So inside the function you write to a memory location using an uninitialized pointer. That's real bad.

2) In the function the inner while loop continues until end-of-string. Then the outer loop increments p and dereferences it. So you read beyond end-of-string which is again real bad.

Try like:

#include <stdio.h>

#define MAX_EXT_LEN 100

void get_extension(char *file_name, char *extension, int sz);

int main(){
    char ex[MAX_EXT_LEN];  // Create an array to store the data

    get_extension("hello.txt", ex, sizeof ex);

    char *p;
    printf("extension: ");
    for(p = ex; *p != '\0'; p++){
        printf("%c", *p);
    }

    return 0;
}

void get_extension(char *file_name, char *extension, int sz){

    char *p = file_name;

   if (file_name == NULL || extension == NULL || sz < 1) return;

    while (*p != '\0' && *p != '.') ++p;

    if(*p == '.') ++p;

    int cnt = 0;
    while(*p != '\0' && cnt < (sz-1)){
        extension[cnt] = *p;
        ++p;
        ++cnt;
    }

    extension[cnt] = '\0';
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63