相关疑难解决方法(0)

如何将wstring转换为字符串?

问题是如何将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)

c++ unicode stl wstring

191
推荐指数
13
解决办法
26万
查看次数

readdir()是否保证订单?

我正在使用opendir/readdir获取类似linux的系统上的文件列表.看来目录条目按文件名的字母顺序返回.但是,我在手册页中看不到有关此订单的任何内容.

谁能告诉我readdir是否保证订单?

c readdir

50
推荐指数
5
解决办法
3万
查看次数

如何在C++中获取目录中的文件列表?

如何获取目录中的文件列表,以便可以处理每个文件?

c++ directory file

45
推荐指数
4
解决办法
12万
查看次数

Visual Studio 2010或2008中的<dirent.h>

我想#include<dirent.h>在我的代码中使用但发生此错误:

无法打开包含文件:'dirent.h':没有这样的文件或目录

我怎么解决这个问题 ?

c++ visual-studio-2010 visual-studio-2008 visual-studio

39
推荐指数
1
解决办法
9万
查看次数

从目录中读取文件名

我想知道在C++中是否有一种简单的方法可以从包含许多文件的文件夹中读取大量文件名.如果有人在想,它们都是位图.

我对Windows编程知之甚少,所以我希望可以使用简单的C++方法完成.

c++ file-io file

16
推荐指数
5
解决办法
2万
查看次数

how to search the computer for files and folders

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.

c++ windows file

13
推荐指数
2
解决办法
4万
查看次数

readdir()返回文件名时,stat()错误'没有这样的文件或目录'

我无法识别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)

c unix stat

8
推荐指数
1
解决办法
2万
查看次数

FindFirstFile的预期输入类型是什么?

我首先要说的是我基本上不了解宽字符串和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++ unicode qt visual-studio-2010

3
推荐指数
1
解决办法
3523
查看次数

std :: system()无法正常工作

如果我在命令行上执行命令"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编译器.

c++ shell command-line cmd system

3
推荐指数
1
解决办法
293
查看次数

C++ 删除所有文件和子文件夹但保留目录本身

我需要删除临时文件夹中的所有内容。我知道我可以使用filesystem::remove_all,filesystem::remove_all_dir但这意味着程序也会删除临时文件夹本身,这当然不是我想要的。我找不到 C++ 的答案,所以如果你们能帮忙,那就太好了。

谢谢!

c++ windows winapi delete-file

3
推荐指数
2
解决办法
5076
查看次数