istream 之 getline

autofield 2012-04-30 10:16:03
想让IO流支持UNICODE ,这么写代码。为何跑起来,一闪而过?求解,代码如下,console application 选择支持MFC。

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
locale("chs");

vector<wstring> strColl;
vector<wstring>::iterator iter;
vector<wstring>::const_iterator const_iter;

TCHAR buffer[2048];
wcout << "Enter a string: ";

while (wcin.getline(buffer, sizeof(buffer)-1))
{
wcout << "string== " << buffer << endl;
strColl.push_back(buffer);
}
wcout << "print vector<string>: \n";
for (const_iter = strColl.begin(); const_iter != strColl.end(); ++const_iter)
{
wcout << (*const_iter).c_str() <<"\n";
}

wcout << endl;
...全文
127 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
autofield 2012-04-30
  • 打赏
  • 举报
回复
最初环境:icl 10.21 stlport5.2.1 编译过,跑起来不对。不知何故。
autofield 2012-04-30
  • 打赏
  • 举报
回复
这么做了,不再用那个TCHAR ARRAY了。没必要,String直接getline,运行OK.
		wstring wstr;
vector<wstring> strColl;
vector<wstring>::iterator iter;
vector<wstring>::const_iterator const_iter;

// TCHAR buffer[2048];
wcout << L"Enter a string: ";

// while (wcin.getline(buffer, sizeof(buffer)-1))
while(getline(std::wcin, wstr))
{
wcout << wstr << endl;
strColl.push_back(wstr);
}
wcout << "print vector<string>: \n";
for (const_iter = strColl.begin(); const_iter != strColl.end(); ++const_iter)
{
wcout << *const_iter << endl;
}

cout << endl;
[Quote=引用 4 楼 的回复:]
win7系统,在程序结束后,等待你再次输入一次,想要让程序暂定,必须自己接收输入。
[/Quote]
autofield 2012-04-30
  • 打赏
  • 举报
回复
那就怪了,2008,这么写,死活编译不过。[Quote=引用 3 楼 的回复:]

C/C++ code

// aaa.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <afx.h>
#include <afxdb.h>
#include <iostream>
#include <vector>

using namespace std;

int _tmain(int argc, TCHAR* argv[], T……
[/Quote]
Qyee16 2012-04-30
  • 打赏
  • 举报
回复
Qyee16 2012-04-30
  • 打赏
  • 举报
回复

win7系统,在程序结束后,等待你再次输入一次,想要让程序暂定,必须自己接收输入。
Qyee16 2012-04-30
  • 打赏
  • 举报
回复

// aaa.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <afx.h>
#include <afxdb.h>
#include <iostream>
#include <vector>

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
locale("chs");

vector<wstring> strColl;
vector<wstring>::iterator iter;
vector<wstring>::const_iterator const_iter;

TCHAR buffer[2048];
wcout << "Enter a string: ";

while (wcin.getline(buffer, sizeof(buffer)-1))
{
wcout << "string== " << buffer << endl;
strColl.push_back(buffer);
}
wcout << "print vector<string>: \n";
for (const_iter = strColl.begin(); const_iter != strColl.end(); ++const_iter)
{
wcout << (*const_iter).c_str() <<"\n";
}

wcout << endl;

}
}


win7, vs2008 运行正常,如下:
taodm 2012-04-30
  • 打赏
  • 举报
回复
不要让流去支持unicode。
W170532934 2012-04-30
  • 打赏
  • 举报
回复
单步调试下呢。

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧