如何从 NSFileHandle 中逐行读取数据?
我有一个文本文件,其中包含给定的数据,
例如
PUFGUjVRallYZDNaazFtVjVObU1zWm5ZcUJUYU5ORk4zbGthNHNDVUdSMlFVQmpSVEoxUUNSallYaFhkanBITXBGR1NTQnpZRTltZE1OalVzSkdXQ0Z6WXR0V2RpTmpTdXgwTWs5V1lZSkZiWjFXT29OV2JSVlhaSTUwYUpwR040UUZXTzVHVXFoWFVRcFdWNHdVTUJ0Q1VHSmxXVlJVTlJCMVE1VTFWV
PUFGUjVRallYZDNaazFtVjVObU1zWm5ZcUJUYU5ORk4zbGthNHNDVUdSMlFVQmpSVEoxUUNSallYaFhkanBITXBGR1NTQnpZRTltZE1OalVzSkdXQ0Z6WXR0V2RpTmpTdXgwTWs5V1lZSkZiWjFXT29OV2JSVlhaSTUwYUpwR040UUZXTzVHVXFoWFVRcFdWNHdVTUJ0Q1VHSmxXVlJVTlJCMVE1VTFWV
现在我想逐行读取数据。这意味着我首先要阅读
PUFGUjVRallYZDNaazFtVjVObU1zWm5ZcUJUYU5ORk4zbGthNHNDVUdSMlFVQmpSVEoxUUNSallYaFhkanBITXBGR1NTQnpZRTltZE1OalVzSkdXQ0Z6WXR0V2RpTmpTdXgwTWs5V1lZSkZiWjFXT29OV2JSVlhaSTUwYUpwR040UUZXTzVHVXFoWFVRcFdWNHdVTUJ0Q1VHSmxXVlJVTlJCMVE1VTFWV
,然后才是剩下的。 有人有什么想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果你的文件很小,那么 @mipadi 的方法可能就可以了。但是,如果您的文件很大(也许> 1MB?),那么您可能需要考虑逐行读取文件。我曾经编写过一个类来执行此操作,我将其粘贴到此处:
然后要使用它,您需要执行以下操作:
或者(对于 10.6+ 和 iOS 4+):
If your file is small, then @mipadi's method will probably be just fine. However, if your file is large (> 1MB, perhaps?), then you may want to consider reading the file line-by-line. I wrote a class once to do that, which I'll paste here:
Then to use this, you'd do:
Or (for 10.6+ and iOS 4+):
我将其重写为符合 ARC 标准:
I rewrote this to be ARC compliant:
我基于 GitHub 项目。 com/users/115730/dave-delong">戴夫·德龙。欢迎您改进代码。现在我可以向前和向后读取文件了。
I started a GitHub project based on the source code of Dave DeLong. You are welcome to improve the code. By now I can read files forwards and backwards.
Cocoa 中没有用于逐行读取文件的 API 或内置语言结构。
There's no API in Cocoa, or built-in language constructs, for reading a file line by line.
对于LARGE文本文件,此问题的答案不需要自定义函数。 Objective-C 是 c 的超集,因此有 c 方法来做到这一点。
请注意, fgetln 不会保留换行符。另外,我们将 str 的长度+1,因为我们想为 NULL 终止留出空间。
The answer to this question for LARGE text files does not require a custom function. Objective-C is a superset of c, and therefore has the c methods to do just this.
Note that fgetln will not keep your newline character. Also, We +1 the length of the str because we want to make space for the NULL termination.
这是我用来从 NSInputStream 读取单个行的方法。请注意,它是针对可读性而非速度进行优化的。 ;-)
Here is a method which I used for reading an individual line from an NSInputStream. Note that it is optimized for readability and not for speed. ;-)
我发现 GitX 也使用行阅读器。
查看 brotherbard 在 GitHub 上的存储库 或 Michael Stapelberg 的网站。
@杨乔
好的!接下来的几天我会仔细看看。
如果您想分叉我在 GitHub 上的存储库和向我发送拉取请求。
I found out that GitX uses a line reader as well.
Checkout the brotherbard's repository on GitHub or the website of the Michael Stapelberg.
@Joe Yang
Nice! I will take a closer look the next days.
I would be glad if you want to fork my repository on GitHub and send me a pull request.
我已将 FileReader 修改为 NSFileHandle 类别,希望它可以帮助其他人
I've modified the FileReader to an NSFileHandle category, hope it could help others
您还可以在 https://github 查看我为 HTTP 服务器项目创建的 CGIStream 库.com/xcvista/ohttpd2/tree/master/CGIStream。此代码不是对文件描述符进行操作,而是对 NSInputStream 进行操作。它本质上是 Microsoft .net 框架中
System.IO.StreamReader
和System.IO.StreamWriter
的 Objective-C 克隆。它不仅可以处理文件,还可以处理网络套接字。我用它来处理 HTTP 协议,该协议与 CGI 前缀同名。
You can also check out the CGIStream library I created for my HTTP server project at https://github.com/xcvista/ohttpd2/tree/master/CGIStream. Instead of file descriptors, this code operates on NSInputStream. It is essentially an Objective-C clone of
System.IO.StreamReader
andSystem.IO.StreamWriter
from Microsoft's .net framework.It will work with not only files but also network sockets. I use it to handle HTTP protocol, which is the namesake of the CGI prefix.
我在其他一些情况下遇到了类似的情况,这是我在 Swift 3 中的解决方案。假设文本文件是 utf8。
这是用法:
https://gist.github.com/codelynx/c1de603a85e7503fe9597d027e93f4de
I run into the similar situation with some other circumstances, and here is my solution in Swift 3. Assuming text file to be utf8.
Here is the usage:
https://gist.github.com/codelynx/c1de603a85e7503fe9597d027e93f4de
这对我在 Swift 5 上有用。
https://gist.github.com/sooop/a2b110f8eebdf904d0664ed171bcd7a2
This worked for me on Swift 5 .
https://gist.github.com/sooop/a2b110f8eebdf904d0664ed171bcd7a2