0

我有两个 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>>'

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

4

1 回答 1

0

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

如果您真的希望在两种配置中编译相同的代码,则必须使用std::basic_string<_TCHAR>and std::basic_ifstream<_TCHAR>,将文字包装在_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 _TCHAR/ME 开发时才有意义。否则,您真的应该只使用 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;
}
于 2014-05-29T17:07:49.557 回答