我是一个C++新手,在网上查了一些文件读取的例子,但是都报错,没有正常使用的,求各位大侠帮下忙,谢谢了。我需要的就功能如下:
STDMETHODIMP CFun::ShowT(BSTR *n1, BSTR *pReturn)
{
}
n1 为要读取的文件绝对路径,然后按这个种径读取到文件内容,在返回。

解决方案 »

  1.   

    oyljerry 你好,我想问一下,fopen能实现逐行读取吗?
    还有 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读出来的是乱码,这是为什么?
      

  2.   

    如果你用的是BSTR为路径的话,那你就用吧
    _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;
    }
      

  3.   

    回复3楼的,不好意思,我是第一次用C++,也是今大才找的很多例子来试着做的,你那个真看不懂。
    // 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上输出都是乱码。
      

  4.   

    注意php网页和com中各自使用什么编码
      

  5.   

    编码问题,
    如果你的输出编码与文件编码不一样就会出现这种问题。
    还有就是,并不是所有文件都可以读取到,因为和PHP执行权限有关
      

  6.   

    我的文本文档里内容是111111111111111111111我想,这个不存在编码问题吧,而且,都是GBK,后来GB2312我也试了。
      

  7.   

    你程序的编码又是什么
    UNICODE还是asni
      

  8.   

    就没有编码问题吗?
    UNICODE 下‘1’与 asni的‘1’所占字节数不一样,所以输出之后就看起来是乱码了
      

  9.   

    好象是我的问题搞错了,那能请问下,怎么才能把a.txt里的全部内容放到 *pReturn 这个变量里呢?