【问题标题】:Running 'iconv' command for a file passed as function arguments in C++ application为在 C++ 应用程序中作为函数参数传递的文件运行“iconv”命令
【发布时间】:2017-09-15 20:47:23
【问题描述】:

我正在尝试将 Windows 文件(CP1252 格式)转换为 Linux 应用程序的 UTF-8 格式。 我想在我的 C++ 应用程序中运行以下命令:

iconv -f CP1252 -t UTF-8 file.ldf |dos2unix > out.ldf

文件名file.ldf 将作为参数传递给main()

例如。

int main (int argc, char* argv[])
{
    string FileName = "Invalid";
    if (argc == 2) {
        FileName = argv[1];
        system("iconv -f CP1252 -t UTF-8 file.ldf |dos2unix > out.ldf");
        //do further parsing on file                        
    }
    else
        cout << "ERROR:: invalid number of arguments"<< endl;
    return 0;
}

我目前面临的问题是将传入的filename 作为要使用system API 执行的命令的一部分。
有没有其他方法可以解决这个问题?

【问题讨论】:

  • 确实知道可以使用+ 运算符附加字符串吗?就像例如"iconv -f CP1252 -t UTF-8 " + FileName + " | dos2unix &gt; out.ldf"
  • 啊!!我现在意识到这确实是一个愚蠢的问题。 :(

标签: c++ string io


【解决方案1】:

改变这个:

system("iconv -f CP1252 -t UTF-8 file.ldf |dos2unix > out.ldf");

到这里:

system("iconv -f CP1252 -t UTF-8 " + FileName + " |dos2unix > out.ldf");

我使用std::string 的重载+ 运算符进行字符串连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 2015-09-12
    • 2013-07-15
    • 2020-02-07
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    相关资源
    最近更新 更多