2013-07-22 13 views
5

ho costruire un'applicazione per Mac, ho 2 problemi:Objective C - Creare file di testo per leggere e scrivere riga per riga in Cocoa

  1. I want to create a text file to read and write data on it. I don't know how to crate a text file to read and write data. Is it use struct?
  2. I want to create a XML file to read and write data on it. Can I create a struct for XML?

Avete suggerimenti? Grazie in anticipo

+0

T dai uno sguardo a http://stackoverflow.com/a/3711079/944634 –

risposta

20

Beh, per creare un file, basta usare

[[NSFileManager defaultManager] createFileAtPath:@"Your/Path" contents:nil attributes:nil]; 

Questo crea un file vuoto, che è possibile scrivere o leggere. Per scrivere il testo (o XML), basta usare il metodo 's NSStringwriteToFile:atomically:encoding:error: come questo

NSString *str = //Your text or XML 
[str writeToFile:"Your/Path" atomically:YES encoding:NSUTF8StringEncoding error:nil]; 

Per leggere da un file, basta fare un NSString con il contenuto di quel file

NSString *contents = [NSString stringWithContentsOfFile:@"Your/Path"]; 

o, se non contiene una stringa, ottenere un oggetto NSData dal file

NSData *contents = [NSData dataWithContentsOfFile:@"Your/Path"]; 
+0

Sai dove si trova il codice di esempio? Grazie – HTKT611

+0

? Che cosa vuoi dire con questo? –

+0

Ho una domanda: Posso usare struct per salvare i dati sui file? – HTKT611

5
/**************************main.m****************************** 
    NS FILE HANDLE READ & WRITE 
    reading and writing in same file 
    Created by abdulsathar on 6/16/14. 
***************************************************************/ 

#import <Foundation/Foundation.h> 
int main(int argc, const char * argv[]) 
{ 
    @autoreleasepool //ARC 
    { 
     NSFileHandle *file; 
     //object for File Handle 
     NSError *error; 
     //crearing error object for string with file contents format 
     NSMutableData *writingdatatofile; 
     //create mutable object for ns data 
     NSString *filePath=[NSString stringWithFormat:@"/Users/chandrakumar/Documents/abdul/doc.txt"]; 
     //telling about File Path for Reading for easy of access 
     file = [NSFileHandle fileHandleForReadingAtPath:@"/Users/chandrakumar/Documents/abdul/doc.txt"]; 
     //assign file path directory 
      if (file == nil) //check file exist or not 
       NSLog(@"Failed to open file"); 
     NSString *getfileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; 
     //access file contents with out ns handle method 
      if (error) //check error flag for file present or not 
       NSLog(@"Error reading file: %@", error.localizedDescription); 
     NSLog(@"contents: %@", getfileContents); 
     //display file contents in main file 
     NSArray *listArray = [getfileContents componentsSeparatedByString:@"\n"]; 
     //caluculate list of line present in files 
     NSLog(@"items = %ld", [listArray count]); 
     const char *writingchar = "how are you"; 
     writingdatatofile = [NSMutableData dataWithBytes:writingchar length:strlen(writingchar)]; 
     //convert string format into ns mutable data format 
     file = [NSFileHandle fileHandleForUpdatingAtPath: @"/Users/chandrakumar/Documents/abdul/new.txt"]; 
     //set writing path to file 
      if (file == nil) //check file present or not in file 
       NSLog(@"Failed to open file"); 
     [file seekToFileOffset: 6]; 
     //object pointer initialy points the offset as 6 position in file 
     [file writeData: writingdatatofile]; 
     //writing data to new file 
     [file closeFile]; 
     //close the file 
    } 
    return 0;`enter code here` 
} 

/***********************************OUTPUT******************************************** 

2014-06-17 14:55:39.695 storage[4075:303] contents: hello how are you my dearservice 

*************************************************************************************/ 
Problemi correlati