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 -
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
example
name: operation.txt
name without extension: operation
15 answers
-
yg_be Posted messages 23437 Registration date Status Contributor Last intervention Ambassadeur 1 588
Hello,
maybe by taking everything before the last point? -
And how do we do that in C?
-
For example, using the functions from string.h https://fr.wikipedia.org/wiki/String.h
-
-
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? -
Hello,
Why would you want to do this in C if, apparently like me, you don't understand anything about it?
In batch, it's simple:
https://forums.commentcamarche.net/forum/affich-14163549-batch-lister-noms-de-ficheirs-sans-extensio -
I know how to do it in bash, but in my exercise, I'm asked to do it in C.
-
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. -
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(…). -
The end of the buffer is the null-terminating character '\0'
-
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 .? -
Thank you, I understand. Thank you for your help.
Actually, I need to create a program to replace .txt files with .as files. -
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 -
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] -
Normally it shouldn't make any difference since memccpy will stop at the character before the dot.
-
And what should I change in my code for it to work?
-
I understand what was wrong, thank you.