我创建了utf-8的文件,为什么我写东西写不了

管哥的天下 2011-12-14 04:44:27

FILE *pf = _tfopen(_T("c:\\22.txt"),_T("wt+,ccs=UTF-8"));
wchar_t* wszString = L"abcd1234你我他fff";
//预转换,得到所需空间的大小,这次用的函数和上面名字相反
int u8Len = ::WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL);
//同上,分配空间要给'\0'留个空间
//UTF8虽然是Unicode的压缩形式,但也是多字节字符串,所以可以以char的形式保存
char* szU8 = new char[u8Len + 2];
//转换
//unicode版对应的strlen是wcslen
::WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), szU8, u8Len, NULL, NULL);
//最后加上'\0'
szU8[u8Len] = '\n';
szU8[u8Len+1] = '\0';
// szU8[u8Len+2] = '\0';
char ccc[]="\xef\xbb\xbf";
int ij = strlen(szU8);
int iii =fwrite(ccc,strlen(ccc), 1, pf);
iii = fwrite(szU8, strlen(szU8), 1, pf);
delete []szU8;
fclose(pf);
return;




iii的值为1???????
...全文
164 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
管哥的天下 2011-12-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhao4zhong1 的回复:]
FILE *pf = _tfopen(_T("c:\\22.txt"),_T("wb,ccs=UTF-8"));
再试试?
不要把
fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待

fopen("...","...b");fread,fwrite,f……
[/Quote]

我试了下,正确,问下为什么换成 wb就行了呢?
追求执着 2011-12-14
  • 打赏
  • 举报
回复
文件读写赵老师的强项啊!!!
赵4老师 2011-12-14
  • 打赏
  • 举报
回复
fwrite
Writes data to a stream.

size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );

Function Required Header Compatibility
fwrite <stdio.h> ANSI, Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

fwrite returns the number of full items actually written, which may be less than count if an error occurs. Also, if an error occurs, the file-position indicator cannot be determined.

Parameters

buffer

Pointer to data to be written

size

Item size in bytes

count

Maximum number of items to be written

stream

Pointer to FILE structure

Remarks

The fwrite function writes up to count items, of size length each, from buffer to the output stream. The file pointer associated with stream (if there is one) is incremented by the number of bytes actually written. If stream is opened in text mode, each carriage return is replaced with a carriage-return – linefeed pair. The replacement has no effect on the return value.

赵4老师 2011-12-14
  • 打赏
  • 举报
回复
FILE *pf = _tfopen(_T("c:\\22.txt"),_T("wb,ccs=UTF-8"));
再试试?
不要把
fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待

fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待
弄混了

whoami1978 2011-12-14
  • 打赏
  • 举报
回复
iii返回为1是正确的啊
fwrite(ccc,strlen(ccc), 1, pf);
你就是写了1个记录(fwrite返回的就是记录数)

64,662

社区成员

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

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