Extract the file name without its extension

Solved
Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   -  
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   -
Hello, is there a function that extracts the file name in C, please?
example

name: operation.txt

name without extension: operation

15 answers

  1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   Ambassadeur 1 588
     
    Hello,
    maybe by taking everything before the last point?
    0
  2. Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   1
     
    And how do we do that in C?
    0
  3. Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   1
     
    I don't know which function to choose; there are really a lot of them and many I don't know.
    Can you tell me which one it is, please?
    0
  4. Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   1
     
    I know how to do it in bash, but in my exercise, I'm asked to do it in C.
    0
  5. Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   1
     
    If someone knows how to do it, I'm taking
    For example, memccpy can do pretty much what I want but it will include the . whereas I don't want a point.
    0
  6. Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   1
     
    In fact, by looking at this code:

     #include <stdio.h> #include <string.h> #include <stdlib.h> char* msg = "This is the string: not copied"; int main( void ) { char buffer[80]; memset( buffer, '\0', 80 ); memccpy( buffer, msg, ':', 80 ); printf( "%s\n", buffer ); return EXIT_SUCCESS; } 


    It returns this is the string:

    I don't know in this code, it's as if we put a ‘\0’ at the end of the buffer, otherwise I don't see the utility of doing memset(…).
    0
    1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      What do you mean by the "end" of the buffer?
      Do you know the conventions in C for representing strings?
      0
  7. Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   1
     
    The end of the buffer is the null-terminating character '\0'
    0
    1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      memset puts 80 \0 in buffer.
      0
  8. Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   1
     
    Ah ok, I see, I understand better

    So, actually
    For example, if I have
    char fichier [10]= "operations.txt"
    fichier sans extension=operations
    Char fichier_sans_extension[10];

    To do this, I do memccpy(fichier_sans_extension,fichier, '.' ,10)
    puts(fichier_sans_extension)
    Normally, this gives me operations.

    How can I ignore the .?
    0
    1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      Maybe test if the last character is a "".", and if so, replace it with \0.

      In what context are you doing this?
      0
  9. Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   1
     
    Thank you, I understand. Thank you for your help.

    Actually, I need to create a program to replace .txt files with .as files.
    0
  10. Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   1
     
    Actually, I wrote this code but it does not work and I don't understand why
     #include <stdio.h> #include <string.h> int main(){ char fichier[]="operations.txt"; char dest[50]; char fichier_sans_extension[50]; int i=0; while(fichier[i]!='\0'){ if(fichier[i]=='.'){ memccpy(fichier_sans_extension,fichier,fichier[i-1],strlen(fichier)); puts(fichier_sans_extension); } i++; } } 


    Do you know what is wrong with my code?

    I
    0
    1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      "It doesn't work": what's happening?

      Your use of memccpy seems particularly risky to me.
      0
  11. Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   1
     
    Actually, earlier it displayed
    operations��U
    operations6�wU
    operationsX�
    .....

    It's weird that it shows me stuff like that

    Actually, I wanted memccpy to return operations

    o p ....s .
    0 1 9 10

    If I use memccpy like before, it returns operations/

    I wanted my program to find the index where the dot is, which is i=9

    And by doing fichier[i-1], it allows me to stop at s

    And doing like before except I replace '.' with fichier[i-1]
    0
    1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      and if there are two s's in the name?
      0
    2. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      Have you read the documentation for memccpy?

      Did you not see recently what some people did before memccpy?
      0
      1. Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   1 > yg_be Posted messages 23437 Registration date   Status Contributor Last intervention  
         
        Ah, I hadn't seen this response before, what did you mean?
        0
  12. Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   1
     
    Normally it shouldn't make any difference since memccpy will stop at the character before the dot.
    0
  13. Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   1
     
    And what should I change in my code for it to work?
    0
  14. Theo_0055 Posted messages 273 Registration date   Status Member Last intervention   1
     
    I understand what was wrong, thank you.
    0
    1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      Could you then mark the discussion as resolved?
      1