不能在一个项目中将argv传递给字符串;在另一个地方工作得很好
Can't pass argv into a string in one project; Works fine in another
我有两个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_t
或char
取决于项目是否设置为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;
}
相关文章:
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 在 c++ 中对字符串进行操作的关系运算符无法按预期工作
- C++ "noskipws"没有按预期工作,如何正确允许字符串中的空格?
- C++20字符串文本模板参数工作示例
- 将双精度值转换为 char 变量时字符串流如何工作
- 为什么在C++中使用关系运算符创建的模板函数不能对字符串正常工作?
- 为什么 C 样式字符串的工作空指针检查不?
- 比较字符串的 GetLine 工作一次,然后比较之后不起作用
- 在 stl 中的字符串中擦除关键字的工作?
- scanf() 语句中"%*[^n]"的格式字符串指示什么?分配抑制器 (*) 和否定扫描集 ([^) 如何协同工作?
- 使用 for 循环打印字符串数组无法正常工作 (C++)
- 字符串文字到 char 数组的转换如何在C++中实际工作
- 如何让我的构造函数和函数工作,以便我的 main() 能够同时显示字符串和 int 数据
- 如何使用字符串更改此插入功能以在链接列表中工作
- cv::split() 在至强处理器上崩溃,但在其他地方工作
- C++如何字符串::比较工作
- "x"如何在字符串中工作?
- 如何让c风格的字符串函数工作
- md5的实现在字符串上工作得很好,但是在文件上不能给出正确的结果
- 不能在一个项目中将argv传递给字符串;在另一个地方工作得很好