我是一个C++新手,在网上查了一些文件读取的例子,但是都报错,没有正常使用的,求各位大侠帮下忙,谢谢了。我需要的就功能如下:
STDMETHODIMP CFun::ShowT(BSTR *n1, BSTR *pReturn)
{
}
n1 为要读取的文件绝对路径,然后按这个种径读取到文件内容,在返回。
STDMETHODIMP CFun::ShowT(BSTR *n1, BSTR *pReturn)
{
}
n1 为要读取的文件绝对路径,然后按这个种径读取到文件内容,在返回。
还有 int MAXLEN = 1024;
FILE* f=fopen("d:\\a.txt","rb");
char* buf[1024];
memset(buf,0,1024); while(!feof(f))
{
int rt= fread(buf,MAXLEN-1,1,f);//每次读取MAXLEN-1字节
//if(rt>0)send(serverAddr,buf,rt,0);
}
*pReturn = BSTR(buf);我这样写,然后PHP读出来的是乱码,这是为什么?
_wfopen#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <wchar.h>#define BUFFER_SIZE 50int main(int argc, char** argv)
{
wchar_t str[BUFFER_SIZE];
size_t strSize;
FILE* fileHandle; // Create an the xml file in text and Unicode encoding mode.
if ((fileHandle = _wfopen( L"_wfopen_test.xml",L"wt+,ccs=UNICODE")) == NULL) // C4996
// Note: _wfopen is deprecated; consider using _wfopen_s instead
{
wprintf(L"_wfopen failed!\n");
return(0);
} // Write a string into the file.
wcscpy_s(str, sizeof(str)/sizeof(wchar_t), L"<xmlTag>\n");
strSize = wcslen(str);
if (fwrite(str, sizeof(wchar_t), strSize, fileHandle) != strSize)
{
wprintf(L"fwrite failed!\n");
} // Write a string into the file.
wcscpy_s(str, sizeof(str)/sizeof(wchar_t), L"</xmlTag>");
strSize = wcslen(str);
if (fwrite(str, sizeof(wchar_t), strSize, fileHandle) != strSize)
{
wprintf(L"fwrite failed!\n");
} // Close the file.
if (fclose(fileHandle))
{
wprintf(L"fclose failed!\n");
}
return 0;
}
// Fun.cpp : Implementation of CFun
#include "stdafx.h"
#include "NFS.h"
#include "Fun.h"/////////////////////////////////////////////////////////////////////////////
// CFunSTDMETHODIMP CFun::ShowT(BSTR *n1, BSTR *pReturn)
{
// TODO: Add your implementation code here
int MAXLEN = 1024;
FILE* f=fopen("d:\\a.txt","rb");
char* buf[1024];
memset(buf,0,1024); while(!feof(f))
{
int rt= fread(buf,MAXLEN-1,1,f);//每次读取MAXLEN-1字节
//if(rt>0)send(serverAddr,buf,rt,0);
}
*pReturn = BSTR(buf); return S_OK;
}这个是我今天找的例子全部了,不知道为什么在PHP上输出都是乱码。
如果你的输出编码与文件编码不一样就会出现这种问题。
还有就是,并不是所有文件都可以读取到,因为和PHP执行权限有关
UNICODE还是asni
UNICODE 下‘1’与 asni的‘1’所占字节数不一样,所以输出之后就看起来是乱码了