【问题标题】:Can't pass argv into a string in one project; Works fine in another不能在一个项目中将 argv 传递给字符串;在另一个工作正常
【发布时间】:2014-05-29 14:31:07
【问题描述】:

我有两个 c++ win32 控制台应用程序项目。两者的代码完全相同。

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    if (argc != 3){

        cout << "Program needs 2 arguments";
    }
    else{
        string filePath = argv[1];
        string flag = argv[2];
        unsigned long int size;
        bool najden = false;

        //odpri datoteko
        ifstream idatoteka(filePath, ios::in | ios::binary | ios::ate);
        if (idatoteka){
            if (flag != "-c" && flag != "-e"){
                cout << "unknown flag";
            }
            else{
                if (flag == "-c"){
                    //kompresija
                }
                else{
                    //dekompresija
                }
            }
        }
        else{
            cout << "This file doesn't exist\n";
        }
    }
    system("pause");
    return 0;
}

愚蠢的是,其中一个给了我一个错误,我尝试将 argv[1] 和 argv[2] 传递给字符串变量。错误信息如下:

cannot convert from '_TCHAR *' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'

这从什么时候开始不起作用,两个相同的项目中的一个怎么可能产生错误?

【问题讨论】:

  • 你可以找到你的答案here
  • 谢谢。问题解决了。但是编译器如何决定何时使用 wstring 以及何时使用 string?代码是相同的。另外,我可以让它默认使用 wstring(不必总是输入 wstring)吗?
  • 在了解了string和wstring之后,通过比较Project属性找到了最终的解决方案。解决方案是将字符集设置为“使用多字节字符集”。然后编译器会让你从 _TCHAR* 传递到字符串。
  • @user1956373 请注意,这种技术(使用 _TCHAR、_tmain 等)必须归类为失败的实验。处理std::wstring 的方式(通常)与处理std::string 的方式大不相同;您必须为每种情况编写不同的代码。

标签: c++ string winapi argv tchar


【解决方案1】:

_TCHAR 定义为 wchar_tchar 取决于项目是否设置为编译为 Unicode。当你有一个正在为 Ansi/MBCS 编译的项目时,_TCHAR*(即char*)可以分配给std::string。当你有一个项目被编译为 Unicode 时,_TCHAR*(即wchar_t*)不能分配给std::string,你必须改用std::wstring,或者在运行时将 Unicode 数据转换为 Ansi/MBCS。

如果您真的希望在两种配置中编译相同的代码,则必须使用std::basic_string&lt;_TCHAR&gt;std::basic_ifstream&lt;_TCHAR&gt;,将文字包装在_T() 中等。例如:

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    if (argc != 3){
        cout << "Program needs 2 arguments";
    }
    else{
        basic_string<_TCHAR> filePath = argv[1];
        basic_string<_TCHAR> flag = argv[2];
        unsigned long int size;
        bool najden = false;

        //odpri datoteko
        basic_ifstream<_TCHAR> idatoteka(filePath, ios::in | ios::binary | ios::ate);
        if (idatoteka){
            if (flag != _T("-c") && flag != _T("-e")){
                cout << "unknown flag";
            }
            else{
                if (flag == _T("-c")){
                    //kompresija
                }
                else{
                    //dekompresija
                }
            }
        }
        else{
            cout << "This file doesn't exist\n";
        }
    }
    system("pause");
    return 0;
}

Windows 长期以来一直是 Unicode 操作系统。只有在为 Windows 9x/ME 开发时,使用 _TCHAR 才有意义。否则,你真的应该只使用 Unicode 而不是 _TCHAR

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int wmain(int argc, WCHAR* argv[])
{
    if (argc != 3){
        wcout << L"Program needs 2 arguments";
    }
    else{
        wstring filePath = argv[1];
        wstring flag = argv[2];
        unsigned long int size;
        bool najden = false;

        //odpri datoteko
        wifstream idatoteka(filePath, ios::in | ios::binary | ios::ate);
        if (idatoteka){
            if (flag != L"-c" && flag != L"-e"){
                wcout << L"unknown flag";
            }
            else{
                if (flag == L"-c"){
                    //kompresija
                }
                else{
                    //dekompresija
                }
            }
        }
        else{
            wcout << L"This file doesn't exist\n";
        }
    }
    system("pause");
    return 0;
}

【讨论】:

    猜你喜欢
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多