【问题标题】:How to merge two or more streams in another or in a file using vectors?如何使用向量合并另一个或文件中的两个或多个流?
【发布时间】:2012-02-08 11:16:16
【问题描述】:

我直接回答这个问题。我通过逐行读取文件然后合并字符串来做到这一点,但需要一个很好的解释如何使用向量来做到这一点。我的目标是使用系统命令(Windows XP、7)来检查目录内容并将它们直接发送到“vector sysoutp”作为示例,然后将其与第二个文件中的信息合并到新文件中。有什么想法吗?

只是我代码的一部分:

while (getline(myinputfile, line))
    {
        lines++;
        found = line.find(countchars);
        if (found!=string::npos)
        {
            line.erase(int(found) + 4);
            myoutputfile << line << endl;
        }
    }

myinputfile 中填充了“syscomm”,这个想法是用向量进行转换,但显然当我尝试做类似getline(cin, tmp) 的事情时,“cin”是 syscomm 没有任何反应(嗯,错误弹出窗口):(

我在使用这个预处理指令时也有错误

#define syscomm system("dir /b | find /i /v ".exe .txt")

createlist.cpp|15|warning: missing terminating " character [enabled by default]|
createlist.cpp|53|error: missing terminating " character|
createlist.cpp||In function 'int main()':|
createlist.cpp|53|error: request for member 'exe' in '"dir /b | find /i /v "', which is of non-class type 'const char [21]'|
||=== Build finished: 2 errors, 1 warnings ===|

它可以在不添加“| find ...”的情况下工作,但使用它我可以使用已知扩展名过滤不需要的文件名。

感谢合作,对二合一问题包感到抱歉:)

【问题讨论】:

  • 请一次一个问题。
  • 那么从命令输出中逐行读取并将它们插入向量中以进行以下排序操作的基本算法是什么?我知道如何进行排序,但阅读内容很难理解......做了:while (!inputcomm.eof()) { sortedvector.push_back(inputcomm.getline(buffer, 10)); } sort(sortedvector.begin(), sortedvector.end()); pclose(inputcomm); return sortedvector; 其中 inputcomm 是例如FILE *inputcomm = popen("dir /b | findstr /i \"\\&lt;*.sd\"", "rt"); :(

标签: c++ string file vector merge


【解决方案1】:

再次检查#define...

#define syscomm system("dir /b | find /i /v ".exe .txt")
// String starts here -^                    ^
// String ends here ------------------------'

如果它有语法着色,它应该在你的编辑器中非常明显。

【讨论】:

  • 是的,我的错误,忘记关闭引号,但它仍然输出 exec 和 txt 文件名:|
  • @1000Gbps 必须对字符串中的双引号进行转义,如下所示:"string \" containing \" four \" double \" quotes"。你也这样做了?
  • 所以我必须使用"dir /b | findstr /i /v \".exe .txt""? 我忘了find 不能使用两个以上的字符串。必须使用findstr,现在可以了:)
猜你喜欢
  • 1970-01-01
  • 2019-01-29
  • 2011-06-03
  • 1970-01-01
  • 2018-11-08
  • 1970-01-01
  • 2015-06-11
  • 2012-10-14
相关资源
最近更新 更多