从.txt文件读取和写入整数 [英] Read and write an integer to/from a .txt file

查看:217
本文介绍了从.txt文件读取和写入整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何读写文本文件中的整数,并且可以读取或写入多行,即处理多个整数? p>感谢。

解决方案

它只是取决于文本文件的确切格式。

读取文本文件的内容很简单:

  //如果你想处理一个错误,不要将NULL传递给下面的代码,而是一个NSError指针。 
NSString * contents = [NSString stringWithContentsOfFile:@/ path / to / fileencoding:NSUTF8StringEncoding error:NULL];

创建一个包含整个文件的自动释放字符串。如果所有的文件包含一个整数,你可以写这个:

  NSInteger integer = [contents integerValue]; 

如果文件分成多行(每行包含一个整数)必须将其分割:

pre $ NS $ array $ {
for(NSString * line in line){
NSInteger currentInteger = [line integerValue];
//用整数做一些事情。



$ b $ p
$ b

总的来说,这很简单。




写回文件同样简单。一旦你操纵了你想要的东西回到一个字符串中,你可以使用这个:
$ b $ pre $ N $ C $ newContents = ... ; //新字符串
[newContents writeToFile:@/ path / to / fileatomically:YES encoding:NSUTF8StringEncoding error:NULL];

您可以使用它来写入字符串。当然,你可以玩设置。将自动设置为 YES 会导致它先写入测试文件,验证它,然后复制它以替换旧文件(这可以确保如果发生故障,您不会以损坏的文件结束)。如果你愿意的话,你可以使用不同的编码(尽管强烈推荐使用 NSUTF8StringEncoding ),如果你想要发现错误(你应该从本质上来说),你可以通过对该方法的 NSError 的引用。它看起来像这样:

pre $ NSError * error = nil;
[newContents writeToFile:@someFile.txtatomically:YES encoding:NSUTF8StringEncoding error:& error];
if(error){
//发生了一些错误。处理它。







如需进一步阅读,请参阅 NSString类参考 a>。


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, i.e., 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). 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. 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.

这篇关于从.txt文件读取和写入整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆