无法将argv传递给一个项目中的字符串;在另一个工作正常

时间:2014-05-29 14:31:08

标签: c++ string winapi argv tchar

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

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

1 个答案:

答案 0 :(得分:0)

_TCHAR定义为wchar_tchar,具体取决于项目是否设置为Unicode编译。当您为Ansi / MBCS编译项目时,_TCHAR*(即char*)可以分配给std::string。当您为Unicode编译项目时,_TCHAR*(即wchar_t*)无法分配给std::string,您必须改为使用std::wstring或转换Unicode数据在运行时到Ansi / MBCS。

如果您真的希望在两种配置中编译相同的代码,则必须使用std::basic_string<_TCHAR>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 / 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;
}