0

代码

while (dataFile)
    {
        getline(dataFile, input, '$');
        if (getenv("windir"))
        {
            cmd = "open -a 'Google Chrome' http://www.dictionary.reference.com/browse/" + input;
        }

        else
        {
            cmd = "open -a 'Safari' http://www.dictionary.reference.com/browse/" + input;
        }
        system(cmd.c_str());
        cmd.erase(cmd.find(input));
        cout << cmd;
    }

当 .txt 文件中有多个单词时,它只会查找第一个单词,而不能查找其他单词。它在输出中说明了这一点sh: line 2: WORD: command not found。我怎样才能让它重置命令,以便其他词起作用?

cmd 和 input 的初始化在 main 方法中。

 string cmd;
 string input;

这是文本文件的样子:

Word1
Word2
WordETC
4

1 回答 1

0

我不能代表 Safari 路径,我假设在 OSX 或类似系统中,但在 Windows 7 上,以下代码将在 Google chrome 中打开一个新选项卡

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main()
{
    std::ifstream dataFile("C:\\test.txt", std::ifstream::binary);

    string input;
    std::string cmd;

    // This path will depend on the operation system
    std::string googlePath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";        

    while (dataFile)
    {
        getline(dataFile, input, '\n'); // use \n for endline

        if (getenv("windir"))
        {
            cmd = "cmd /C start \"" + googlePath + "\" http://www.dictionary.reference.com/browse/" + input + " --newtab";
        }

        else
        {
            cmd = "open -a 'Safari' http://www.dictionary.reference.com/browse/" + input;
        }
        system(cmd.c_str());
        cmd.erase(cmd.find(input));
        cout << cmd;
    }

    return 0;
}

测试.txt

turtle\r\n
dove\r\n
shoulder
于 2013-09-07T05:00:41.017 回答