C ++如何在写入时向buf添加消息?

时间:2017-12-22 12:11:10

标签: c++

我是C ++的新手。需要帮忙。 在C ++中我们有这个功能

$scope.gridOptions = {
    ...
    showFooter: true,
    footerTemplate: getCustomFooterTemplate(),
    ...
};

function getCustomFooterTemplate() {
        return "footerTemplate.html",
            '<div ng-show="showFooter" class="ngFooterPanel" ng-class="{\'ui-widget-content\': jqueryUITheme, ' +
            '\'ui-corner-bottom\': jqueryUITheme}" ng-style="footerStyle()">\n' +
            '   <div class="ngTotalSelectContainer" >\n' +
            '       <div class="ngFooterTotalItems" ng-class="{\'ngNoMultiSelect\': !multiSelect}" >\n' +
            '           <span class="ngLabel">{{i18n.ngTotalItemsLabel}} {{maxRows()}}</span>' +
            '           <span ng-show="filterText.length > 0" class="ngLabel">({{i18n.ngShowingItemsLabel}} ' +
            '           {{totalFilteredItemsLength()}})</span>\n' +
            '       </div>\n' +
            '       <div class="ngFooterSelectedItems" ng-show="multiSelect">\n' +
            '           <span class="ngLabel">{{i18n.ngSelectedItemsLabel}} {{selectedItems.length}}</span>\n' +
            '       </div>\n' +
            '   </div>\n' +
            '   <div class="ngPagerContainer" style="float: right; margin-top: 10px;" ng-show="enablePaging" ' +
            '   ng-class="{\'ngNoMultiSelect\': !multiSelect}">\n' +
            '       <div style="float:left; margin-right: 10px;" class="ngRowCountPicker">\n' +
            '           <span style="float: left; margin-top: 3px;" class="ngLabel">{{i18n.ngPageSizeLabel}}</span>\n' +
            '           <select style="float: left;height: 27px; width: 100px" ng-model="pagingOptions.pageSize" >\n' +
            '               <option ng-repeat="size in pagingOptions.pageSizes">{{size}}</option>\n' +
            '           </select>\n' +
            '       </div>\n' +
            '       <div style="float:left; margin-right: 10px; line-height:25px;" class="ngPagerControl" ' +
            '       style="float: left; min-width: 135px;">\n' +
            '           <button type="button" class="ngPagerButton" ng-click="pageToFirst()" ' +
            '           ng-disabled="cantPageBackward()" title="{{i18n.ngPagerFirstTitle}}">' +
            '           <div class="ngPagerFirstTriangle"><div class="ngPagerFirstBar"></div></div></button>\n' +

            '           <button type="button" class="ngPagerButton" ng-click="pageBackward()" ' +
            '           ng-disabled="cantPageBackward()" title="{{i18n.ngPagerPrevTitle}}"><div ' +
            '           class="ngPagerFirstTriangle ngPagerPrevTriangle"></div></button>\n' +

            '           <label style="vertical-align: top; font-weight: inherit;">{{pagingOptions.currentPage}} to {{totalPages}} of {{maxRows()}}</label>\n' +

            '           <button type="button" class="ngPagerButton" ng-click="pageForward()" ' +
            '           ng-disabled="cantPageForward()" title="{{i18n.ngPagerNextTitle}}"><div ' +
            '           class="ngPagerLastTriangle ngPagerNextTriangle"></div></button>\n' +

            '           <button type="button" class="ngPagerButton" ng-click="pageToLast()" ' +
            '           ng-disabled="cantPageToLast()" title="{{i18n.ngPagerLastTitle}}">' +
            '           <div class="ngPagerLastTriangle"><div class="ngPagerLastBar"></div></div></button>\n' +
            '       </div>\n' +
            '   </div>\n' +
            '</div>\n';
    };

我如何组合“Some string”和str,并把它写成这样写的?

write(int fd, const void *buf, size_t n);
write(1,str,10);
write(1,"Some string\n",13);

C ++中的任何方式?

这是答案: 任何“+”都不适用于此功能。

我自己找到正确的答案

write(1,"Some string "+str,13); //like JS
write(1,"Some string ".str,13); //like PHP

1 个答案:

答案 0 :(得分:1)

这是一个c-String:

"Text"

C-Strings是原始数据类型char的数组。连接运算符+不适用于数组。

这是C ++标准字符串(类std::string的对象):

std::string("Text")

std::string定义了连接运算符+

因此你应该试试这个:

write(1,std::string("Some string ")+str,13);

通过给定的c-string创建std::string的新实例。很少有例外,你必须使用c字符串,所以总是喜欢

std::string str;

char[...] str;
char* str

不要忘记#include <string>

如果您需要有关字符串的更多帮助,请查看this