简体   繁体   English

从/ .txt文件读取和写入整数

[英]Read and write an integer to/from a .txt file

How can I read and write an integer to and from a text file, and is it possible to read or write to multiple lines, ie, deal with multiple integers? 如何在文本文件中读取和写入整数,是否可以读取或写入多行,即处理多个整数?

Thanks. 谢谢。

This is certainly possible; 这当然是可能的; it simply depends on the exact format of the text file. 它只取决于文本文件的确切格式。
Reading the contents of a text file is easy: 阅读文本文件的内容很简单:

// If you want to handle an error, don't pass NULL to the following code, but rather an NSError pointer.
NSString *contents = [NSString stringWithContentsOfFile:@"/path/to/file" encoding:NSUTF8StringEncoding error:NULL];

That creates an autoreleased string containing the entire file. 这会创建一个包含整个文件的自动释放字符串。 If all the file contains is an integer, you can just write this: 如果包含的所有文件都是整数,那么您可以这样写:

NSInteger integer = [contents integerValue];

If the file is split up into multiple lines (with each line containing one integer), you'll have to split it up: 如果文件被分成多行(每行包含一个整数),则必须将其拆分:

NSArray *lines = [contents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
for (NSString *line in lines) {
    NSInteger currentInteger = [line integerValue];
    // Do something with the integer.
}

Overall, it's very simple. 总的来说,它非常简单。


Writing back to a file is just as easy. 写回文件同样容易。 Once you've manipulated what you wanted back into a string, you can just use this: 一旦你将你想要的东西操作回一个字符串,你就可以使用它:

NSString *newContents = ...; // New string.
[newContents writeToFile:@"/path/to/file" atomically:YES encoding:NSUTF8StringEncoding error:NULL];

You can use that to write to a string. 您可以使用它来写入字符串。 Of course, you can play with the settings. 当然,您可以使用设置。 Setting atomically to YES causes it to write to a test file first, verify it, and then copy it over to replace the old file (this ensures that if some failure happens, you won't end up with a corrupt file). atomically设置为YES会导致它首先写入测试文件,验证它,然后将其复制以替换旧文件(这可确保如果发生某些故障,您将不会得到损坏的文件)。 If you want, you can use a different encoding (though NSUTF8StringEncoding is highly recommended), and if you want to catch errors (which you should, essentially), you can pass in a reference to an NSError to the method. 如果需要,可以使用不同的编码(虽然强烈建议使用NSUTF8StringEncoding ),如果要捕获错误(本质上应该是这样),可以将NSError的引用传递给方法。 It would look something like this: 它看起来像这样:

NSError *error = nil;
[newContents writeToFile:@"someFile.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (error) {
    // Some error has occurred. Handle it.
}

For further reading, consult the NSString Class Reference . 有关进一步阅读,请参阅NSString类参考

If you have to write to multiple lines, use \\r\\n when building the newContents string to specify where line breaks are to be placed. 如果必须写入多行,请在构建newContents字符串时使用\\r\\n指定要放置换行符的位置。

NSMutableString *newContents = [[NSMutableString alloc] init];

for (/* loop conditions here */)
{
    NSString *lineString = //...do stuff to put important info for this line...
    [newContents appendString:lineString];
    [newContents appendString:@"\r\n"];
}

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

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