简体   繁体   English

Objective C - 创建文本文件,在Cocoa中逐行读写

[英]Objective C - Create text file to read and write line by line in Cocoa

I building a Mac app,I have 2 problem: 我构建一个Mac应用程序,我有2个问题:

  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? 它是否使用struct?
  2. I want to create a XML file to read and write data on it. 我想创建一个XML文件来读取和写入数据。 Can I create a struct for XML? 我可以为XML创建结构吗?

Do you have suggestion? 你有什么建议吗? Thanks in advance 提前致谢

Well, to create a file, just use 好吧,要创建一个文件,只需使用

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

This creates an empty file, which you can write to or read from. 这会创建一个空文件,您可以写入或读取该文件。 To write text (or XML), just use NSString 's writeToFile:atomically:encoding:error: method like this 要编写文本(或XML),只需使用NSStringwriteToFile:atomically:encoding:error:方法

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

To read from a file, just make an NSString with the contents of that file 要从文件中读取,只需使用该文件的内容创建一个NSString

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

or, if it does not contain a string, get an NSData object from the file 或者,如果它不包含字符串,则从文件中获取NSData对象

NSData *contents = [NSData dataWithContentsOfFile:@"Your/Path"];
/**************************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

*************************************************************************************/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM