c++ - gsoap c 文件读取

标签 c++ file gsoap

我是 C++ 新手。我已经用c#编写了上传文件wcf,并使用gsoap2.8生成header.h文件。

这是我用c#编写的上传文件wcf服务。

public void UploadFile(RemoteFileInfo request)
    {
        FileStream targetStream = null;
        Stream sourceStream = request.FileByteStream;

        string uploadFolder = @"C:\temp\upload\copyHere";
        string filePath = Path.Combine(uploadFolder, request.FileName);

        using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            //read from the input stream in 6K chunks
            //and save to output stream
            const int bufferLen = 65000;
            byte[] buffer = new byte[bufferLen];
            int count = 0;
            while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
            {
                targetStream.Write(buffer, 0, count);
            }
            targetStream.Close();
            sourceStream.Close();
        }

    }

下面是 gsoap2.8 从 wsdl 生成的 header.h 代码:

class SOAP_CMAC xsd__base64Binary
{
public:
    unsigned char *__ptr;
    int __size;
    char *id;   /* optional element of type xsd:string */
    char *type; /* optional element of type xsd:string */
    char *options;  /* optional element of type xsd:string */
    struct soap *soap;  /* transient */
public:
    virtual int soap_type() const { return 8; } /* = unique id SOAP_TYPE_xsd__base64Binary */
    virtual void soap_default(struct soap*);
    virtual void soap_serialize(struct soap*) const;
    virtual int soap_put(struct soap*, const char*, const char*) const;
    virtual int soap_out(struct soap*, const char*, int, const char*) const;
    virtual void *soap_get(struct soap*, const char*, const char*);
    virtual void *soap_in(struct soap*, const char*, const char*);
             xsd__base64Binary() { xsd__base64Binary::soap_default(NULL); }
    virtual ~xsd__base64Binary() { }
};

class SOAP_CMAC _ns1__RemoteFileInfo
{
public:
    xsd__base64Binary FileByteStream;   /* SOAP 1.2 RPC return element (when namespace qualified) */    /* required element of type ns2:StreamBody */
    struct soap *soap;  /* transient */
public:
    virtual int soap_type() const { return 14; } /* = unique id SOAP_TYPE__ns1__RemoteFileInfo */
    virtual void soap_default(struct soap*);
    virtual void soap_serialize(struct soap*) const;
    virtual int soap_put(struct soap*, const char*, const char*) const;
    virtual int soap_out(struct soap*, const char*, int, const char*) const;
    virtual void *soap_get(struct soap*, const char*, const char*);
    virtual void *soap_in(struct soap*, const char*, const char*);
             _ns1__RemoteFileInfo() { _ns1__RemoteFileInfo::soap_default(NULL); }
    virtual ~_ns1__RemoteFileInfo() { }
};

这是我的 test.cpp 代码。我需要将文件读入 xsd__base64Binary 以便我可以分配给remoteFile.FileByteStream

BasicHttpBinding_USCOREITransferService svc;
__ns1__UploadFileResponse uploadRespond;
_ns1__RemoteFileInfo remoteFile;

std::string path = "C:\\temp\\upload\\test.txt";

我需要将上面的文件读入 ????

remoteFile.FileByteStream = ?

这样我就可以调用Web服务uploadFile,如下所示:

int result = svc.__ns1__UploadFile(&remoteFile, uploadRespond);

提前谢谢您,

JH

最佳答案

这是一个 c 语言的例子:

    unsigned char * buffer; 
    int fileLen;    
    FILE *file;

    //OPEN FILE
    file = fopen(name, "rb");
    if (!file)
    {
        fprintf(stderr, "Unable to open file %s", name);
        return;
    }

    //GET FILE LENGTH
    fseek(file, 0, SEEK_END);
    fileLen=ftell(file);
    fseek(file, 0, SEEK_SET);

    //ALLOCATE MEMORY
    buffer=(unsigned char *)malloc(fileLen+1);
    if (!buffer)
    {
        fprintf(stderr, "Memory error! :( ");
        fclose(file);
        return;
    }

    //READ FILE CONTENTS INTO BUFFER AND CLOSE FILE
    fread(buffer, fileLen, 1, file);
    fclose(file);

    //USE THE BUFFER ....


    //FREE YOUR ALLOCATED BUFFER
    free(buffer);

在您的remoteFile中:

remoteFile.FileByteStream.__ptr = buffer;
remoteFile.FileByteStream.__size = fileLen;

更新:使用soap_malloc分配内存

用途:

buffer=(unsigned char *)soap_malloc(soap,fileLen+1);

而不是:

buffer=(unsigned char *)malloc(fileLen+1);

删除:

free(buffer);

分配将在当前操作的上下文中完成,释放将在操作完成时由 gsoal 库完成。

关于c++ - gsoap c 文件读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14285315/

相关文章:

c++ - OpenCV 2.4.2 : Undefined References

c++ - 发送数据lwip STM32 PC

Java - 使用哈希和数组匹配比较两个文件

c++ - XSD 生成的解析器中的 gSOAP 替代方案

c++ - std::map 中 vector 值的迭代器

c++ - 按字符读取字符串直到行尾 C/​​C++

C - sscanf 在读取文件后给我错误的输出

c# - 在 C# 中使用 FileSystemWatcher 确定复制/写入进度

soap - WSDL 最佳实践

c++ - 以 "generic"方式设置成员属性