当控制台处于UTF-8模式时,来自popen()的文本被过早切断

时间:2012-06-09 22:31:33

标签: c++ windows console popen

#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

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

也许我忘记将这里的某些功能更改为多字节替换?

0 个答案:

没有答案