wstring类的问题。

aksufign 2015-06-26 12:02:34
代码如下:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <tchar.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
wstring x = L"abcdefg";
wcout << x << endl;
wcout << x.c_str() << endl; // 上下两句输出结果一样,没问题。
wifstream fin("raw.txt", ios::in | ios::binary);
if (!fin.is_open())
{
wcerr << "Error." << endl;
return 0;
}

wofstream fout("out.txt", ios::out | ios::binary);
if (!fout.is_open())
{
wcerr << "Error." << endl;
return 0;
}
wstring w_str;
if (!fin.is_open())
{
cout << "Error." << endl;
return 0;
}
while (!fin.eof())
{

getline(fin, w_str);
wcout << w_str << endl;
wcout << w_str.c_str() << endl;
// 这里,在读取一行以后,上下两句输出结果不同了,是什么原因?
}

return 0;
}
...全文
297 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-06-29
  • 打赏
  • 举报
回复
引用 8 楼 Xy_betray 的回复:
[quote=引用 6 楼 zhao4zhong1 的回复:] 项目、属性、配置属性、常规、字符集:使用Unicode
读取之前是不是需要把UNICODE文件开头的FF FE跳过去呢?[/quote] 有这个可能。 C++不知道。 C参考下面: In Visual C++ 2005, fopen supports Unicode file streams. A flag specifying the desired encoding may be passed to fopen when opening a new file or overwriting an existing file, like this: fopen("newfile.txt", "rw, ccs=<encoding>"); Allowed values of the encoding include UNICODE, UTF-8, and UTF16-LE. If the file is already in existence and is opened for reading or appending, the Byte Order Mark (BOM) is used to determine the correct encoding. It is not necessary to specify the encoding with a flag. In fact, the flag will be ignored if it conflicts with the type of the file as indicated by the BOM. The flag is only used when no BOM is present or if the file is a new file. The following table summarizes the modes used in for various flags given to fopen and Byte Order Marks used in the file. Encodings Used Based on Flag and BOM Flag No BOM (or new file) BOM: UTF-8 BOM: UTF-16 UNICODE ANSI UTF-8 UTF-16LE UTF-8 UTF-8 UTF-8 UTF-16LE UTF-16LE UTF-16LE UTF-8 UTF-16LE Generic-Text Routine Mappings TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined _tfopen fopen fopen _wfopen
aksufign 2015-06-29
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
项目、属性、配置属性、常规、字符集:使用Unicode
读取之前是不是需要把UNICODE文件开头的FF FE跳过去呢?
aksufign 2015-06-29
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
项目、属性、配置属性、常规、字符集:使用Unicode
赵老师,这个已经设置好。 文件-高级保存选项里面也保存了UNICODE了。。。。然后还是这样
赵4老师 2015-06-29
  • 打赏
  • 举报
回复
项目、属性、配置属性、常规、字符集:使用Unicode
aksufign 2015-06-29
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <tchar.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    wstring x = L"abcdefg";
    wcout << x << endl;
    wcout << x.c_str() << endl; // 上下两句输出结果一样,没问题。
    wifstream fin("raw.txt", ios::in);
    if (!fin.is_open())
    {
        wcerr << L"Error." << endl;
        return 0;
    }

    wofstream fout("out.txt", ios::out);
    if (!fout.is_open())
    {
        wcerr << L"Error." << endl;
        return 0;
    }
    wstring w_str;
    if (!fin.is_open())
    {
        wcout << L"Error." << endl;
        return 0;
    }
    while (!fin.eof())
    {

        getline(fin, w_str);
        wcout << w_str << endl;
        wcout << w_str.c_str() << endl;
        // 这里,在读取一行以后,上下两句输出结果不同了,是什么原因?
    }
    
    return 0;
}

请问赵老师,结果一样输出错误。 wcout << w_str.c_str() << endl; 这一句还是只能输出一个问号。是不是我的环境配置有问题? VS2013操作UNICODE文件需要如何配置?
赵4老师 2015-06-29
  • 打赏
  • 举报
回复 1
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <tchar.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    wstring x = L"abcdefg";
    wcout << x << endl;
    wcout << x.c_str() << endl; // 上下两句输出结果一样,没问题。
    wifstream fin("raw.txt", ios::in);
    if (!fin.is_open())
    {
        wcerr << L"Error." << endl;
        return 0;
    }

    wofstream fout("out.txt", ios::out);
    if (!fout.is_open())
    {
        wcerr << L"Error." << endl;
        return 0;
    }
    wstring w_str;
    if (!fin.is_open())
    {
        wcout << L"Error." << endl;
        return 0;
    }
    while (!fin.eof())
    {

        getline(fin, w_str);
        wcout << w_str << endl;
        wcout << w_str.c_str() << endl;
        // 这里,在读取一行以后,上下两句输出结果不同了,是什么原因?
    }
    
    return 0;
}

dgwreghwr 2015-06-28
  • 打赏
  • 举报
回复
我在raw.txt里面随便敲了几行,调试过程中发现w_str字符串中间会出现’\0’字符,我觉得原因有可能是wcout在打印wstring的时候是把其中所有的内容打印出来,而在打印wchar_t*类型的字符串时,遇到’\0’字符就停止打印了
aksufign 2015-06-27
  • 打赏
  • 举报
回复
能指点下吗?多谢大家了。
aksufign 2015-06-26
  • 打赏
  • 举报
回复
raw.txt文件为UNICODE编码。

64,648

社区成员

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

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