【问题标题】:Finding a string in a .txt and getting the line contents in C++ [closed]在 .txt 中查找字符串并在 C++ 中获取行内容 [关闭]
【发布时间】:2012-12-06 00:18:16
【问题描述】:

我正在寻找一种方法来打开文本文件并找到某个字符串,然后获取它所在的行,然后获取该行的内容。我找不到任何帮助。我还需要使用 C++ 和预安装的库...等任何无法在 Mac 上运行或未预装 C++ 的东西...。我也许可以使用其他小的库...

【问题讨论】:

  • 有很多关于如何做到这一点的信息。你试过什么?
  • 谷歌,Stackoverflow 我什么都找不到?一个链接至少会有所帮助....
  • 您将不得不将点点滴滴结合起来,没有人会为您提供确切问题的解决方案。但是 cplusplus.com 上的 fileIO 文档(cplusplus.com/doc/tutorial/files)可能足以读取文件,当然还有 find 方法来查看字符串 A 是否包含字符串 B 有据可查。 cplusplus.com/reference/string/string/find

标签: c++ file


【解决方案1】:
open a filestream

while filestream is NOT at End Of File
    get a line of the file into a temp string
    search the temp string for the keyword
    if the keyword is in it
        thats your line, exit the loop
    else keep looping

希望这对你来说是一个足够大的提示。

【讨论】:

  • 天哪,谢谢!我永远不会猜到......糟糕的算法...... Thnx
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-29
  • 1970-01-01
  • 2014-07-10
  • 1970-01-01
  • 2013-12-23
  • 2016-07-24
  • 1970-01-01
相关资源
最近更新 更多