Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2756 connectés 

  FORUM HardWare.fr
  Programmation
  C++

  utilisation et modification de nom de fichier

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

utilisation et modification de nom de fichier

n°388943
theorie du​ chaos
morte et enterrée
Posté le 10-05-2003 à 10:31:58  profilanswer
 

#include <stdio.h>
 
 int main (char *argv[]) {
 char nom_listing [100] ;
 
 strcpy (nom_listing, argv[1]) ;
 printf ("%s", nom_listing) ;
 strcat (nom_listing, ".l" ) ;
 printf ("%s", nom_listing) ;
 
 // unlink (listing);
}  
 
 
en fait je voudrais  utiliser le nom_de_fichier pointe par argv[1]
afin de creer un nouveau fichier s appelant nom_de_fichier.l
 
le probleme doit etre dans mon utilisation de argv[1]
j obtiens Bus Error (core dumped)


---------------
Les accents sont en option... j'ai un clavier qwertz.
mood
Publicité
Posté le 10-05-2003 à 10:31:58  profilanswer
 

n°388946
Taz
bisounours-codeur
Posté le 10-05-2003 à 10:37:51  profilanswer
 

Code :
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. int main(int argc, char **argv)
  5. {
  6.   char *nom=NULL;
  7.   int i;
  8.   for(i=1; i<argc; ++i)
  9.     {
  10.       /* sizeof ".l" == sizeof '.' + sizeof 'l' + sizeof '\0' */
  11.       nom=realloc(nom, strlen(argv[i])+sizeof ".l" );
  12.       strcpy(nom, argv[i]);
  13.       strcat(nom, ".l" );
  14.       puts(nom);
  15.     }
  16.   free(nom);
  17.   return 0;
  18. }


Message édité par Taz le 10-05-2003 à 10:46:02
n°388954
theorie du​ chaos
morte et enterrée
Posté le 10-05-2003 à 11:04:15  profilanswer
 

est ce qu il est possible d obtenir le meme resultat avec srpintf en gardant non comme tableau de caracteres et en faisant un unlink apres?


---------------
Les accents sont en option... j'ai un clavier qwertz.
n°388959
Taz
bisounours-codeur
Posté le 10-05-2003 à 11:17:19  profilanswer
 

oui
 
remplace
 
strcpy(nom, argv[i]);
strcat(nom, ".l" );  
 
par
 
sprintf(nom, "%s.l", argv[i]);

n°388964
theorie du​ chaos
morte et enterrée
Posté le 10-05-2003 à 11:42:41  profilanswer
 

merci :)


---------------
Les accents sont en option... j'ai un clavier qwertz.
n°388965
gilou
Modérateur
Modzilla
Posté le 10-05-2003 à 11:52:13  profilanswer
 

Si effectivement tu veux créer le fichier .l a partir du nom original, tu devrais faire qque chose dans le genre suivant:
 

Code :
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <sys/stat.h>
  4. #include <unistd.h>
  5. int main (int argc, char **argv)
  6. {
  7.   if (argc > 1)
  8.     {
  9.       char *path = getcwd(NULL, 0);
  10.       if (path)
  11.         {
  12.          char *nom_listing = (char *) malloc(strlen(path)+strlen(argv[1])+ 4);
  13.          stat statbuffer;
  14.          if (nom_listing)
  15.            {
  16.              strcpy(nom_listing, path);
  17.              strcat(nom_listing, "/" );
  18.              strcpy(nom_listing, argv[1]);
  19.              strcat(nom_listing, ".l" );
  20.              if (stat(nom_listing, &statbuffer))
  21.                {
  22.                  FILE *fichier = fopen(nom_listing, "a+" );
  23.                  if (fichier)
  24.                      fclose(fichier);
  25.                }
  26.              free(nom_listing);
  27.           }
  28.         free(path);
  29.         }
  30.      }
  31. exit(0); 
  32. }


 
Tu verifies que tu as un nom en parametre, tu recuperes le repertoire courant, tu crees un nom absolu de fichier listing a partir du repertoire courant et de ton nom de fichier, tu verifies que le fichier correspondant n'existe pas, et s'il n'existe pas, tu le crée.
A+,


Message édité par gilou le 10-05-2003 à 11:55:34

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°388966
Taz
bisounours-codeur
Posté le 10-05-2003 à 11:53:26  profilanswer
 

on paralias de C ANSI jusqu'à présent

n°388971
gilou
Modérateur
Modzilla
Posté le 10-05-2003 à 11:59:25  profilanswer
 

++Taz a écrit :

on paralias de C ANSI jusqu'à présent

Vu l'utilisation en commentaire de unlink dans le post initial, je subodore que c'est pour Unix/Linux qu'elle écrit son code.
A+,


Message édité par gilou le 10-05-2003 à 11:59:56

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°388972
Taz
bisounours-codeur
Posté le 10-05-2003 à 12:01:22  profilanswer
 

ben y a remove en ANSI    [:spamafote]

n°388976
gilou
Modérateur
Modzilla
Posté le 10-05-2003 à 12:07:23  profilanswer
 

++Taz a écrit :

ben y a remove en ANSI    [:spamafote]  


remove et unlink ne sont pas pareil: remove encapsule a la fois unlink pour les nom de fichiers et rmdir pour les noms de repertoires).  
A priori, s'il y a un remove, il y a aussi un unlink, non?
A+,
PS: Lirai la suite ce soir, la je vas baffrer, puis je vas sur Paris.


Message édité par gilou le 10-05-2003 à 12:08:21

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
mood
Publicité
Posté le 10-05-2003 à 12:07:23  profilanswer
 

n°388981
Taz
bisounours-codeur
Posté le 10-05-2003 à 12:13:49  profilanswer
 

non, c'est l'inverse. remove ne vire que les fichiers puisque la notion de répertoire n'existe pas en C.
 
par contre unlink fait plus
voir le man 2 unlink

n°389011
theorie du​ chaos
morte et enterrée
Posté le 10-05-2003 à 13:28:59  profilanswer
 

je dois programmer en C un assembleur qui tourne sous Linux


---------------
Les accents sont en option... j'ai un clavier qwertz.
n°389014
Taz
bisounours-codeur
Posté le 10-05-2003 à 13:31:41  profilanswer
 

ben si les fonctions ANSI te satisfont, utilise les. sinon prends celles de l'API

n°389046
gilou
Modérateur
Modzilla
Posté le 10-05-2003 à 14:12:11  profilanswer
 

++Taz a écrit :

non, c'est l'inverse. remove ne vire que les fichiers puisque la notion de répertoire n'existe pas en C.
 
par contre unlink fait plus
voir le man 2 unlink


Ce n'est pas ce que dit la man page unix:
 

Citation :

REMOVE (3)
delete a name and possibly the file it refers to
 
SYNOPSIS
#include <stdio.h> int remove(const char * pathname );  
 
DESCRIPTION
remove deletes a name from the filesystem. It calls unlink for files, and rmdir for directories.  
If the removed name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse.  
 
If the name was the last link to a file but any processes still have the file open the file will remain in existence until the last file descriptor referring to it is closed.  
 
If the name referred to a symbolic link the link is removed.  
 
If the name referred to a socket, fifo or device the name for it is removed but processes which have the object open may continue to use it.  
 
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.  
 
ERRORS
EFAULT  
 
pathname points outside your accessible address space.  
 
EACCES  
 
Write access to the directory containing pathname is not allowed for the process's effective uid, or one of the directories in pathname did not allow search (execute) permission.  
 
EPERM  
 
The directory containing pathname has the sticky-bit ( S_ISVTX ) set and the process's effective uid is neither the uid of the file to be deleted nor that of the directory containing it.  
 
ENAMETOOLONG  
 
pathname was too long.  
 
ENOENT  
 
A directory component in pathname does not exist or is a dangling symbolic link.  
 
ENOTDIR  
 
A component used as a directory in pathname is not, in fact, a directory.  
 
ENOMEM  
 
Insufficient kernel memory was available.  
 
EROFS  
 
pathname refers to a file on a read-only filesystem.  
 
CONFORMING TO
ANSI C, SVID, AT&T, POSIX, X/OPEN, BSD 4.3  
 
BUGS
In-felicities in the protocol underlying NFS can cause the unexpected disappearance of files which are still being used.  
 
NOTE
Under libc4 and libc5, remove was an alias for unlink (and hence would not remove directories).  
 
SEE ALSO
- unlink (2) - - rename (2) - - open (2) - - rmdir (2) - - mknod (2) - - mkfifo (3) - - link (2) - - rm (1) - unlink(8) man3/insque 3 man3/resolver 3 man3/resolver 3 man3/resolver 3 man3/resolver 3 man3/resolver 3 man3/resolver 3


 
 
 

Citation :

UNLINK (2)
delete a name and possibly the file it refers to
 
SYNOPSIS
#include <unistd.h> int unlink(const char * pathname );  
 
 
DESCRIPTION
unlink deletes a name from the filesystem. If that name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse.  
If the name was the last link to a file but any processes still have the file open the file will remain in existence until the last file descriptor referring to it is closed.  
 
If the name referred to a symbolic link the link is removed.  
 
If the name referred to a socket, fifo or device the name for it is removed but processes which have the object open may continue to use it.  
 
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.  
 
 
ERRORS
EFAULT  
 
pathname points outside your accessible address space.  
 
EACCES  
 
Write access to the directory containing pathname is not allowed for the process's effective uid, or one of the directories in pathname did not allow search (execute) permission.  
 
EPERM  
 
The directory containing pathname has the sticky-bit ( S_ISVTX ) set and the process's effective uid is neither the uid of the file to be deleted nor that of the directory containing it, or pathname is a directory.  
 
ENAMETOOLONG  
 
pathname was too long.  
 
ENOENT  
 
A directory component in pathname does not exist or is a dangling symbolic link.  
 
ENOTDIR  
 
A component used as a directory in pathname is not, in fact, a directory.  
 
EISDIR  
 
pathname refers to a directory.  
 
ENOMEM  
 
Insufficient kernel memory was available.  
 
EROFS  
 
pathname refers to a file on a read-only filesystem.  
 
ELOOP  
 
Too many symbolic links were encountered in translating pathname .  
 
EIO  
 
An I/O error occurred.  
 
CONFORMING TO
SVr4, SVID, POSIX, X/OPEN, 4.3BSD. SVr4 documents additional error conditions EBUSY, EINTR, EMULTIHOP, ETXTBUSY, ENOLINK.  
 
 
BUGS
Infelicities in the protocol underlying NFS can cause the unexpected disappearance of files which are still being used.  
 
 
SEE ALSO
- link (2) - - rename (2) - - open (2) - - rmdir (2) - - mknod (2) - - mkfifo (3) - - remove (3) - - rm (1) -


 :whistle:  
A+,


Message édité par gilou le 10-05-2003 à 14:23:12

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°389067
Taz
bisounours-codeur
Posté le 10-05-2003 à 14:51:14  profilanswer
 

oui mais c'est là une extension. fo faire gaffe aux man. y a ecrit ansi quand la focnton existe en ANSI, mais le comportement est souvent étendu. donc prudence

n°389567
gilou
Modérateur
Modzilla
Posté le 11-05-2003 à 09:39:15  profilanswer
 

Sauf que dans ce cas precis, remove a ete inventée bien apres unlink et rmdir. Alors parler d'extension...
 
En fait, au départ, remove devait juste remplacer unlink; c'est suite a des questions posées au comité de standardisation qu'elle a aussi remplacé rmdir. Voir par exemple ce document: http://www.pasc.org/interps/unoffi [...] .1-59.html
Personellement, quite a se conformer a un standard, je preferre POSIX.
A+,


Message édité par gilou le 11-05-2003 à 09:39:38

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°389568
Taz
bisounours-codeur
Posté le 11-05-2003 à 09:41:00  profilanswer
 

mai fait ce qui te plait

n°389569
gilou
Modérateur
Modzilla
Posté le 11-05-2003 à 09:46:59  profilanswer
 

Et pis de toute facon, Microsoft (le standard de facto, non? :D)préconise l'emploi de System.IO.File.Delete

Code :
  1. [Visual Basic]
  2. Public Shared Sub Delete( _
  3.    ByVal path As String _
  4. )
  5. [C#]
  6. public static void Delete(
  7.    string path
  8. );
  9. [C++]
  10. public: static void Delete(
  11.    String* path
  12. );
  13. [JScript]
  14. public static function Delete(
  15.    path : String
  16. );


[:pioupiou]  
A+,


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  utilisation et modification de nom de fichier

 

Sujets relatifs
[Delphi] Récupérer la taille d'un (gros) fichier[php] Obtenir le contenu d'une ligne précise d'un fichier
Upload de fichier sans remplacement du ficher existant ?[opengl]une erreur a la compil, ai-je oublié un fichier d'en-tête ?
[C++] Utilisation de timer sous Visual C++ avec API Win32Comment effacer le contenu d'un fichier texte???
script qui recupere des données ds un fichier excelPERL : recuperer les 250 premiers caracteres d'un fichier...
Commet savoir si le pointeur de fichier se trouve sur une ligne vide ?[C++ et MFC] fichier ini
Plus de sujets relatifs à : utilisation et modification de nom de fichier


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR