不能在一个项目中将argv传递给字符串;在另一个地方工作得很好

Can't pass argv into a string in one project; Works fine in another

本文关键字:字符串 地方工作 很好 另一个 一个 不能 项目 argv      更新时间:2023-10-16

我有两个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 existn";
        }
    }
    system("pause");
    return 0;
}

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

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

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

_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 existn";
        }
    }
    system("pause");
    return 0;
}

Windows作为Unicode操作系统已经很长时间了。使用_TCHAR只有在为Windows 9x/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 existn";
        }
    }
    system("pause");
    return 0;
}