中文文件的读取

用fopen_s()函数读取中文文件时会出现乱码。

1.在C++中可以使用:_wfopen_s()函数去打开中文文件。

函数: _wfopen_s(FILE**fp,L"文件路径",L"文件的使用方式")


2.使用:fgetws()函数去读取中文文件。

函数:fgetws(wchar_t*wstr,int len,FILE*fp);

参数:wchar_t*wstr   // 是自己定义的一块数组

          int len   // 是数组读取的大小

          FILE*fp  //FILE类型的对象


3.使用:fputws()函数去写入中文文件。

函数:fputws(wchar_t*wstr,int len,FILE*fp);

参数:wchar_t*wstr  //保存文字的数组

          int len  //数组所保存的文字的数量

          FILE*fp  //FILE类型的对象


实例:读取 D:\QQPCmgr\Desktop\新建文本文档.txt 文件,显示在窗口上,并将内容写入 D:\QQPCmgr\Desktop\新建文本文档2.txt     这个新的文件中。

int main()
{

        FILE*fp;
FILE*fpwrite;
int open1=_wfopen_s(&fp,L"D:\\QQPCmgr\\Desktop\\新建文本文档.txt",L"r");
int open2=_wfopen_s(&fpwrite,L"D:\\QQPCmgr\\Desktop\\新建文本文档2.txt",L"a");
wchar_t linex[100];
if(fp==NULL||fpwrite==NULL)
{
cout<<"open failed!"<<endl;
return 0;
}
while (!feof(fp))  
{  
fgetws(linex,100,fp);  
wcout<<linex;  
fputws(linex,fpwrite);
}  

fclose(fp);  

        fclose(fpwrite);  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值