IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Objective-C Discussion :

Vider un fichier [Objective-C]


Sujet :

Objective-C

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2009
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 25
    Points : 16
    Points
    16
    Par défaut Vider un fichier
    Bonjour, est-ce qu'il existe une methode pour vider le contenu d'un fichier a part ecrire un NSString vide dedans. Je me suis apercu que dans mon code le contenu du fichier etait concatene.
    Merci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
        [[NSFileManager defaultManager] copyItemAtPath:@"/Users/Crea/Documents/MonFichier.txt" toPath:@"/Users/Crea/Documents/MonFichier.txt.bak" error:nil]];
     
        NSMutableString *fileContent = [NSString stringWithContentsOfFile:@"/Users/Crea/Documents/MonFichier.txt" encoding:NSASCIIStringEncoding error:nil];
     
        NSString *newContent = [fileContent stringByReplacingOccurrencesOfString:@"ancien" withString:@"nouveau" options:NSLiteralSearch range:NSMakeRange(0 ,[fileContent length])];
     
        [[NSFileManager defaultManager] createFileAtPath:@"/Users/Crea/Documents/MonFichier.txt" contents:newContent attributes:nil];

  2. #2
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 937
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 937
    Points : 4 358
    Points
    4 358
    Par défaut
    Citation Envoyé par Piscium Voir le message
    Bonjour, est-ce qu'il existe une methode pour vider le contenu d'un fichier a part ecrire un NSString vide dedans. Je me suis apercu que dans mon code le contenu du fichier etait concatene.
    Merci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
        [[NSFileManager defaultManager] copyItemAtPath:@"/Users/Crea/Documents/MonFichier.txt" toPath:@"/Users/Crea/Documents/MonFichier.txt.bak" error:nil]];
     
        NSMutableString *fileContent = [NSString stringWithContentsOfFile:@"/Users/Crea/Documents/MonFichier.txt" encoding:NSASCIIStringEncoding error:nil];
     
        NSString *newContent = [fileContent stringByReplacingOccurrencesOfString:@"ancien" withString:@"nouveau" options:NSLiteralSearch range:NSMakeRange(0 ,[fileContent length])];
     
        [[NSFileManager defaultManager] createFileAtPath:@"/Users/Crea/Documents/MonFichier.txt" contents:newContent attributes:nil];
    1. votre code ne se compile pas : donc ce que vous montrez n'est pas ce que vous exécutez
    2. createFileAtPath ne prend pas une NSString pour l'argument contents

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2009
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 25
    Points : 16
    Points
    16
    Par défaut
    Il y a un crochet en trop a la fin de la premiere ligne, je vais plutot utiliser la methode "dataUsingEncoding:" pour l'argument "contents" c'est plus correcte, autrement le code compile et fonctionne sauf que le nouveau fichier fait le double en poids que le fichier d'origine.

  4. #4
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 937
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 937
    Points : 4 358
    Points
    4 358
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
     
    #import <Cocoa/Cocoa.h>
     
    int main(int argc, char *argv[])
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init] ;
    	NSError *error = nil ;
     
    	[[NSFileManager defaultManager] createFileAtPath:@"/tmp/text.txt" contents:[@"blabla ancien blabla" dataUsingEncoding:NSASCIIStringEncoding] attributes:nil];
    	NSLog(@"oldContent %@",[NSString stringWithContentsOfFile:@"/tmp/text.txt" encoding:NSASCIIStringEncoding error:&error]) ;
     
            // you should really delete the .bak file if it exists before doing this
    	[[NSFileManager defaultManager] copyItemAtPath:@"/tmp/text.txt" toPath:@"/tmp/text.txt.bak" error:nil];
     
    	NSMutableString *fileContent = [NSString stringWithContentsOfFile:@"/tmp/text.txt" encoding:NSASCIIStringEncoding error:nil];
     
    	NSString *newContent = [fileContent stringByReplacingOccurrencesOfString:@"ancien" withString:@"nouveau" options:NSLiteralSearch range:NSMakeRange(0 ,[fileContent length])];
     
    	[[NSFileManager defaultManager] createFileAtPath:@"/tmp/text.txt" contents:[newContent dataUsingEncoding:NSASCIIStringEncoding] attributes:nil];
    	NSLog(@"newContent %@",[NSString stringWithContentsOfFile:@"/tmp/text.txt" encoding:NSASCIIStringEncoding error:&error]) ;
    	NSLog(@"bakContent %@",[NSString stringWithContentsOfFile:@"/tmp/text.txt.bak" encoding:NSASCIIStringEncoding error:&error]) ;
     
    	[pool drain] ;
     
    	return 0 ;
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    2011-07-01 10:10:35.711 TestFiles[32093:a0f] oldContent blabla ancien blabla
    2011-07-01 10:10:35.735 TestFiles[32093:a0f] newContent blabla nouveau blabla
    2011-07-01 10:10:35.736 TestFiles[32093:a0f] bakContent blabla ancien blabla
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    # wc /tmp/text.txt
           0       3      21 /tmp/text.txt
    votre problème ne vient pas du code que vous montrez.

  5. #5
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2009
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 25
    Points : 16
    Points
    16
    Par défaut
    merci pour votre aide, mon code devra etre applique a un fichier ".strings". Le probleme c'est qu'avec l'encodage ASCII je perd le format original donc le texte n'est plus structure et ne veux plus rien dire (caracteres et symbols changes) et en UTF8 le fichier n'est pas ecrit (0 Ko).
    PS: je code pour iOS

  6. #6
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 937
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 937
    Points : 4 358
    Points
    4 358
    Par défaut
    Citation Envoyé par Piscium Voir le message
    merci pour votre aide, mon code devra etre applique a un fichier ".strings". Le probleme c'est qu'avec l'encodage ASCII je perd le format original donc le texte n'est plus structure et ne veux plus rien dire (caracteres et symbols changes) et en UTF8 le fichier n'est pas ecrit (0 Ko).
    PS: je code pour iOS
    les fichiers .strings sont censés être en UTF-16.

    (donc 2 bytes par caractère…)

  7. #7
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2009
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 25
    Points : 16
    Points
    16
    Par défaut
    Citation Envoyé par JeitEmgie Voir le message
    les fichiers .strings sont censés être en UTF-16.

    (donc 2 bytes par caractère…)
    Tout fonctionne nikel maintenant, merci JeitEmgie

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [DOM] Vider une fichier xml
    Par romaik dans le forum Format d'échange (XML, JSON...)
    Réponses: 3
    Dernier message: 11/07/2007, 17h21
  2. vider un fichier texte
    Par choko62 dans le forum VB 6 et antérieur
    Réponses: 22
    Dernier message: 20/06/2007, 21h48
  3. [DEBUTANT] Vider les fichiers temporaires d'un utilisateur
    Par yepAccess dans le forum Windows Serveur
    Réponses: 11
    Dernier message: 04/05/2007, 12h42
  4. Vider un fichier ouvert
    Par dj.motte dans le forum C++
    Réponses: 4
    Dernier message: 27/07/2006, 02h44
  5. Vider 1 fichier sans le supprimer
    Par wodel dans le forum Langage
    Réponses: 2
    Dernier message: 08/07/2006, 16h03

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo