在 c++ 中搜索分隔符时读取文件块的最佳方法?

发布于 2025-01-10 02:15:53 字数 267 浏览 5 评论 0原文

我想根据换行符的数量将一个文件分成几个新文件。目前,我只逐字节读取,在每次读取时查找换行符。我的问题是我觉得我的方法太慢了。我不想调用任何函数。关于每次读取调用应该执行多大的块,我是否应该遵循一条规则?

我在想也许我应该分块读取,然后迭代每个块寻找换行符并将光标设置在该换行符之后。不过,我不确定这如何在运行时节省我的时间。使用 read 调用逐字节读取是否会比我迭代每个块花费更长的时间?

另外,IO库之间有区别吗?我听说过 IO 流的工作原理,但不知道它比 C 的 IO 系统调用库更好。

I want to split a file into several new files depending on the number of new line characters there are. Currently, I only read byte by byte looking for the newline character at each read. My issue is that I feel that my method is too slow. I do not want to call any functions. Is there a rule that I should follow in regards to how big of a chunk at each read call I should be doing?

I was thinking that maybe I should read in chunks and then iterate over each chunk looking for the newline character and setting the cursor to just after that newline character. Though, I am not sure as to how this may save me time in terms of runtime. Is reading byte by byte using the read call gonna take longer than me iterating over each chunk?

Also, are there differences between IO libraries? I've heard of how IO streams work, but don't see how that may be better than the IO system call libraries from C.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文