1
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <windows.h>


using namespace std;
int main() {
  SetConsoleOutputCP(CP_UTF8);
  wstringstream wss;
  wchar_t buff[256];
  FILE *in;
  if(!(in = _wpopen(L"dir %windir%", L"r"))) { return 1; }
  while(!feof(in)) {
    if (fgetws(buff, sizeof(buff), in)!=NULL){
      wss << buff;
    }
  }
  pclose(in);
  wstring wstr;
  wstr = wss.str();
  wcout << wstr.c_str();
}

效果如下:

[...]

2011-04-10  14:11    <DIR>          .
2011-04-10  14:11    <DIR>          ..
2011-04-10  14:11    <DIR>          system32
2011-04-10  14:11    <DIR>          system
2011-04-10  14:11    <DIR>          repair
2011-04-10  14:11    <DIR>          Help
2011-04-10  14:11    <DIR>          Config
2011-04-10  14:11    <DIR>          Cursors
2011-04-10  14:11    <DIR>          Media
2011-04-10  14:11    <DIR>          java
2011-04-10  14:11    <DIR>          Web
2011-04-10  14:11    <DIR>          addins
2012-06-09  11:29             2

似乎文件结尾字符以某种方式在字符串中传递或什么......我不知道。但是,如果我将编码改回一个字节,一切正常..

也许我忘了在这里将某些功能更改为多字节替代方案?

4

0 回答 0