问题是如何将wstring转换为字符串?
我有下一个例子:
#include <string>
#include <iostream>
int main()
{
std::wstring ws = L"Hello";
std::string s( ws.begin(), ws.end() );
//std::cout <<"std::string = "<<s<<std::endl;
std::wcout<<"std::wstring = "<<ws<<std::endl;
std::cout <<"std::string = "<<s<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
注释掉的输出是:
std::string = Hello
std::wstring = Hello
std::string = Hello
Run Code Online (Sandbox Code Playgroud)
但不仅仅是:
std::wstring = Hello
Run Code Online (Sandbox Code Playgroud)
这个例子有什么不对吗?我可以像上面那样进行转换吗?
编辑
新的例子(考虑到一些答案)是
#include <string>
#include <iostream>
#include <sstream>
#include <locale>
int main()
{
setlocale(LC_CTYPE, "");
const std::wstring ws = L"Hello";
const std::string s( ws.begin(), ws.end() );
std::cout<<"std::string = "<<s<<std::endl;
std::wcout<<"std::wstring = "<<ws<<std::endl;
std::stringstream ss;
ss …Run Code Online (Sandbox Code Playgroud) 我正在使用opendir/readdir获取类似linux的系统上的文件列表.看来目录条目按文件名的字母顺序返回.但是,我在手册页中看不到有关此订单的任何内容.
谁能告诉我readdir是否保证订单?
我想#include<dirent.h>在我的代码中使用但发生此错误:
无法打开包含文件:'dirent.h':没有这样的文件或目录
我怎么解决这个问题 ?
我想知道在C++中是否有一种简单的方法可以从包含许多文件的文件夹中读取大量文件名.如果有人在想,它们都是位图.
我对Windows编程知之甚少,所以我希望可以使用简单的C++方法完成.
i need a way to search the computer for files like Windows Explorer. i want my program to search lets say hard drive c:. i need it to search C:\ for folders and files (just the ones you could see in c:\ then if the user clicks on a file on the list like the folder test (C:\test) it would search test and let the user see what files/folders are in it.
我无法识别stat抛出的错误.以下程序读取目录中的所有文件并打印文件名:
DIR *dp;
struct dirent *dirp;
struct stat sb;
if((dp = opendir(argv[1]))==NULL)
{
perror("can't open dir");
}
while((dirp = readdir(dp))!=NULL)
{
if (stat(dirp->d_name, &sb) == -1) {
perror("stat");
}
printf("File name: %s \n",dirp->d_name);
}
Run Code Online (Sandbox Code Playgroud)
样本输出:
/home/eipe
stat error: No such file or directory
File name: copyofsample
File name: a.out
File name: .
stat error: No such file or directory
File name: udpclient.c
File name: ..
stat error: No such file or directory
File name: client.c
stat error: No such file …Run Code Online (Sandbox Code Playgroud) 我首先要说的是我基本上不了解宽字符串和Unicode支持.我让QString和QFile在99%的时间内为我处理这个问题,但我正在尝试编译为VC6编写的其他人的库.
当我在Qt Creator中使用MSVC2010进行编译时,出现此错误:
error: C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Run Code Online (Sandbox Code Playgroud)
代码正在使用FindFirstFile函数,该函数是重载的(取决于您是否使用Unicode字符集进行编译).FindFirstFile当FindFirstFileA和FindFirstFileW的输入似乎是两种完全不同的类型时,我不明白期望什么类型.
所以这是我的问题:预期的输入类型是FindFirstFile什么?
推论:我如何获取类型的文件名const char*并将其放入FindFirstType将接受的表单中?
如果我在命令行上执行命令"C:\ Windows\system32\java.exe",我将获得有关如何使用Java的信息.但是,当我写一个简单的c ++程序来做同样的事情
#include <iostream>
int main()
{
std::system("C:\\Windows\\system32\\java.exe");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到以下控制台输出:
'C:\Windows\system32\java.exe' is not recognized as an internal or external command,
operable program or batch file.
Run Code Online (Sandbox Code Playgroud)
我在Windows 10上使用mingw-w64编译器.
我需要删除临时文件夹中的所有内容。我知道我可以使用filesystem::remove_all,filesystem::remove_all_dir但这意味着程序也会删除临时文件夹本身,这当然不是我想要的。我找不到 C++ 的答案,所以如果你们能帮忙,那就太好了。
谢谢!