gpt4 book ai didi

c++ - 如果从文件流读取数据,为什么 gSOAP 将标准输入模式设置为二进制?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:44:22 25 4
gpt4 key购买 nike

我一直在玩 gSOAP XML 数据绑定(bind),方法是将 XML 文档加载到 C++ 类中,修改数据并将其序列化回 XML。

这是 XML 的片段 - library.xml:

<?xml version="1.0" encoding="UTF-8"?>    
<gt:Library xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:gt="http://www.bk.com/gSOAP/test">
<gt:Books>
<gt:Book isbn="0132350882" author="Robert C. Martin" title="Clean Code">
<gt:CopiesAvailable>2</gt:CopiesAvailable>
</gt:Book>
<gt:Book isbn="020161622X" author="Andrew Hunt" title="The Pragmatic Programmer">
<gt:CopiesAvailable>0</gt:CopiesAvailable>
</gt:Book>
<gt:Book isbn="0201633612" author="Erich Gamma" title="Design patterns">
<gt:CopiesAvailable>1</gt:CopiesAvailable>
</gt:Book>
</gt:Books>
...
</gt:Library>

以下代码将 XML 加载到对象中,修改对象并将其序列化回 XML。请注意,XML 是通过文件流从文件中加载的,要添加的数据是通过标准输入 (cin) 从用户处获取的。

主要.cpp:

#include "soapH.h"
#include "gt.nsmap"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::fstream;
using std::string;
using std::stringstream;

void DisplayAllBooks(const _gt__Library& library)
{
cout << "\n\nDisplaying all books in the library:" << endl;

std::vector<_gt__Library_Books_Book>::const_iterator it = library.Books.Book.begin();
for(;it != library.Books.Book.end(); it++)
{
cout << "\nBook:\n" << "\tTitle:" << (*it).title << "\n\tAuthor:" << (*it).author <<"\n\tISBN: " << (*it).isbn << "\n\tCopies available: " << static_cast<int>((*it).CopiesAvailable) << endl;
}
}

void AddBook(_gt__Library& library)
{
cout << "\n\nAdding a new book:" << endl;

_gt__Library_Books_Book book;

cout << "\tTitle: " << std::flush;
getline(cin, book.title);

cout << "\tAuthor: " << std::flush;
getline(cin, book.author);

cout << "\tISBN:" << std::flush;
getline(cin, book.isbn);

cout << "\tCopies available: " << std::flush;
string strCopiesAvailable;
getline(cin, strCopiesAvailable);
stringstream ss(strCopiesAvailable);
ss >> book.CopiesAvailable;

library.Books.Book.push_back(book);
}

// Terminate and destroy soap
void DestroySoap(struct soap* pSoap)
{
// remove deserialized class instances (C++ objects)
soap_destroy(pSoap);

// clean up and remove deserialized data
soap_end(pSoap);

// detach context (last use and no longer in scope)
soap_done(pSoap);
}

int main()
{

//
// Create and intialize soap
//

// gSOAP runtime context
struct soap soap;

// initialize runtime context
soap_init(&soap);

// Set input mode
soap_imode(&soap, SOAP_ENC_XML);

// reset deserializers; start new (de)serialization phase
soap_begin(&soap);

//
// Load XML (Deserialize)
//

_gt__Library library;
string strXML = "library.xml";

ifstream fstreamIN(strXML);
soap.is = &fstreamIN;

// calls soap_begin_recv, soap_get__gt__Library and soap_end_recv
if(soap_read__gt__Library(&soap, &library) != SOAP_OK)
{
std::cout << "soap_read__gt__Library() failed" << std::endl;
DestroySoap(&soap);
return 1;
}

fstreamIN.close();

//
// Display books before and after adding a new book
//

DisplayAllBooks(library);
AddBook(library);
DisplayAllBooks(library);

//
// Serialize
//

soap_set_omode(&soap, SOAP_XML_INDENT);

ofstream fstreamOUT("library.xml");
soap.os = &fstreamOUT;

// calls soap_begin_send, soap_serialize, soap_put and soap_end_send
if(soap_write__gt__Library(&soap, &library) != SOAP_OK)
{
std::cout << "soap_write__gt__Library() failed" << std::endl;
DestroySoap(&soap);
return 1;
}

fstreamOUT.close();

DestroySoap(&soap);

return 0;
}

运行此测试应用程序后,一切正常,除了所有新添加的元素都有以回车符结尾的字符串(CR - ):

修改后的 XML 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<gt:Library xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:gt="http://www.bk.com/gSOAP/test">
<gt:Books>
<gt:Book isbn="0132350882" author="Robert C. Martin" title="Clean Code">
<gt:CopiesAvailable>2</gt:CopiesAvailable>
</gt:Book>
<gt:Book isbn="020161622X" author="Andrew Hunt" title="The Pragmatic Programmer">
<gt:CopiesAvailable>0</gt:CopiesAvailable>
</gt:Book>
<gt:Book isbn="0201633612" author="Erich Gamma" title="Design patterns">
<gt:CopiesAvailable>1</gt:CopiesAvailable>
</gt:Book>
<gt:Book isbn="12345678&#xD;" author="Scott Meyers&#xD;" title="Effective C++&#xD;">
<gt:CopiesAvailable>123</gt:CopiesAvailable>
</gt:Book>
</gt:Books>
...
</gt:Library>

我追查了错误的来源,发现了以下内容:

soap_read__gt__Library() 调用 soap_begin_send() 执行以下行:

_setmode(soap->recvfd, _O_BINARY);

soap->recvfdsoap_init() 中设置为 0 并且 0 是文件的值stdin 的描述符。

一旦 stdin 的模式更改为二进制,STL 库不会将 \r\n 解析为单个 \n读取操作和 getline(cin, str) 像往常一样读取所有内容直到 \n,将 \r 复制到输出字符串中。这正是出现在最终 XML 中的新字符串中的回车符。

我的问题是:如果数据源是文件流,为什么 gSOAP 会修改 stdin 模式?这是 gSOAP 中的错误吗?

注意:

正如预期的那样,如果 stdio 的模式在 soap_begin_send() 之后但在从 std 读取数据之前恢复为 _O_TEXT::cingetline() 工作正常。这是补丁:

_setmode(_fileno(stdin), _O_TEXT)

最佳答案

这是为了避免在读写 Unicode 和 UTF-8 编码的 XML 时出现编码问题。

关于c++ - 如果从文件流读取数据,为什么 gSOAP 将标准输入模式设置为二进制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9195635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com