c++ 不支持中文路径、中文文件名问题 : _wsystem

参考:

https://blog.csdn.net/ken2232/article/details/130301356

-------------------------------------------------------

https://blog.csdn.net/ken2232/article/details/130316198
QString::toWCharArray 与 宽字符路径、空格 (测试OK)(***)

=====================

//示例1

//可能需要包含

#include "process.h"
#include<stdlib.h>

         

_wsystem(L"测试\\test.exe");

// OK !!!!!!!!!!!!!!!

//示例2

// 关键代码

//\" \" 表示空格,两个\"之间有一个空格

const wchar_t* abc = L"C:/test/新建文件夹\" \"-\" \"副本/cmd.exe";
_wsystem(abc);

1. 使用无 const 报错:wchar_t* abc = L"C:/test/新建文件夹\" \"-\" \"副本/cmd.exe";

2. _wsystem应该是 win OS下系统自带的库函数,无法修改?

参考1:

C++基础(十七)宽字符、wchar_t、wcout、中文乱码/不输出_c++ wcout_yedawei_1的博客-CSDN博客

想要输出宽字节的中文字符,需要设置一个区域,最好的办法是用C++的locale,当然也可以用c语言的setlocale,但不推荐。

修改后的代码如下:

    #include <iostream>
     
    int main()
    {
        using namespace std;
     
        const wchar_t* pwValue_en = L"I Love C++";
        const wchar_t* pwValue_zh = L"Yes!我也爱C++";
        wcout.imbue(locale("zh_CN"));  //设置语言环境
        wcout << pwValue_en << endl;
        wcout << pwValue_zh << endl;
     
        system("pause");
        return 0;
    }

————————————————
版权声明:本文为CSDN博主「yedawei_1」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yedawei_1/article/details/113769765

=============================

疑问:在编译阶段完成,意味着,无法简单地使用变量?

1. 直接赋予常量

wchar_t wt[] = L"白日做梦梦";   //大写字母L告诉编译器为"中"字分配两个字节的空间

2. 预分配一个固定长度的数组,变化的内容存储在数组中,相当于变量。

3. 使用第三方的数据类型

wstackstring wcmd;  //Example #1

4. 使用第三方的库函数

5. 自定义等等

2. wchar_t操作

标准C中的wprintf()函数以及标准C++的iostream类库中的类和对象能提供wchar_t宽字符类型的相关操作。

#include <iostream>
#include <locale>           //setlocale函数在locale头文件中定义
using namespace std;

int main()
{
	//使用setlocale函数将本机的语言设置为中文简体
	//LC_ALL表示设置所有的选项(包括金融货币、小数点,时间日期格式、语言字符串的使用习惯等),chs表示中文简体
	setlocale(LC_ALL, "chs");
	wchar_t wt[] = L"白日做梦梦";   //大写字母L告诉编译器为"中"字分配两个字节的空间
	wcout << wt << endl;               //使用wcout来代替cout输出宽字符
	return 0;
}

===============================

C++ (Cpp) _wsystem Examples - HotExamples

bool importX264Cfg()
{
    wchar_t cmdToRun[MAX_PATH];

         
    ZeroMemory(cmdToRun, sizeof(wchar_t)*MAX_PATH);
    StringCchPrintf(cmdToRun, MAX_PATH*sizeof(wchar_t), L"REG IMPORT %s", _szX264Cfg);

         
    _wsystem(cmdToRun);

         
    return true;
}

------------------------

cpp-docs/system-wsystem.md at main · MicrosoftDocs/cpp-docs · GitHub

RoutineRequired header
system<process.h> or <stdlib.h>
_wsystem<process.h> or <stdlib.h> or <wchar.h>

===============================

c++ 不支持中文路径、中文文件名问题

https://blog.csdn.net/ken2232/article/details/130237805

c++内调用命令行 不支持中文路径

https://zhidao.baidu.com/question/1540522402766798627.html

windows上你可以尝试使用_wsystem,传递宽字符路径。

windows上你可以尝试使用_wsystem,传递宽字符路径。

比如

_wsystem(L"测试\\test.exe");

 不知道你在传递给system函数时,是否将路径的分隔符转义了?如果没有转义,肯定会出现问题。确保代码中的字符串是:

"\"c:\\testing\\中文目录\\空格 目录\\testing.exe\"

system("\"c:\\testing\\中文目录\\空格 目录\\testing.exe\"");

c++ system 运行程序 中文路径

c++ system 运行程序 中文路径_百度知道

如果路径里中文也不是不可以。
一个程序,去调用另一个程序的话,可以用创建进程的方法来调用。
用CreateProcess函数来调用B程序。
你可以查一下CreateProcess的用法。
路径里有中文是没有问题的。

遇到了C/C++控制台程序无法输入中文的情况

https://blog.csdn.net/MYMarcoreus/article/details/117002371

【完美解决:绝对路径含中文问题】bat一键启动应用绝对路径中含中文导致无法执行

https://blog.csdn.net/chengxuyuan99/article/details/109699726

====================================

(Cpp) _wsystem Examples

C++ (Cpp) _wsystem Examples - HotExamples

bool importX264Cfg()
{
	wchar_t cmdToRun[MAX_PATH];
	ZeroMemory(cmdToRun, sizeof(wchar_t)*MAX_PATH);
	StringCchPrintf(cmdToRun, MAX_PATH*sizeof(wchar_t), L"REG IMPORT %s", _szX264Cfg);
	_wsystem(cmdToRun);
	return true;
}

BOOST_NOWIDE_DECL int boost::nowide::system(char const *cmd)
{
    if(!cmd)
        return _wsystem(0);
    wstackstring wcmd;
    if(!wcmd.convert(cmd))
    {
        errno = EINVAL;
        return -1;
    }
    return _wsystem(wcmd.c_str());
}

int u8system(const char *command)
{
	wchar_t *wbuf;
	int r = -1;

	wbuf = u8stows(command);
	if(wbuf) {
		r = _wsystem(wbuf);
		free(wbuf);
	}

	return r;
}

// BrainUtil::CreateFile(_T("C:\\newFolderRoot\\newFolder\\newFile.txt"));
bool BrainUtil::CreateFile(const CString& fileFullName)
{
    if(DoesFileorFolderExist(fileFullName))
        return true;

    CString folderFullName = GetParentFolderName(fileFullName);
    if(!DoesFileorFolderExist(folderFullName))
    {
        bool bRet = CreateFolder(folderFullName); // Create the parent directory tree.
        if(!bRet)
            return false;
    }

    CString name = fileFullName;
    CString cmd;
    cmd.Format(_T("echo \"\" >> \"%s\""), name.GetBuffer());
    int ret = _wsystem(cmd.GetBuffer());
    DATA_ASSERT(0 == ret);
    bool bSucc = DoesFileorFolderExist(fileFullName);
    return bSucc;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值