如何获取字符串的总内容? [英] How to get total content of string?

查看:128
本文介绍了如何获取字符串的总内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经传递一个printf格式的字符串到函数,并尝试使用sscanf()分隔该字符串的内容。但是下面是使用这个函数的问题。
我已经调用函数如下:

  int var = -1; 
ostringstream ossi;
ossi<< 我的内容;
std :: string detailString =我的第一行\\\

第二行内容是alos存在\\\

我们也得到s sdjhf sf
myFunction 什么值:%d \\\
我的内容:%s \\\
详细内容:%s,var,ossi.str()。c_str(),detailString.c_str

实现如下:

  void myFunction(const char * format,...)
{

int var;
char myContent [10000];
char detailContent [50000];
if(sscanf(myString,什么值:%d \\\
我的内容:%s \\\
详细内容:%s,& var,cmyContent,detailContent)== 3)
{
std :: cout<<####<< std :: endl;
std :: cout<<var value is:<< var<< std :: endl;
std :: cout<<我的内容是:<<< myContent<< std :: endl;
std :: cout<<detailContent is:<<< detailContent<< std :: endl;
std :: cout <<####<< std :: endl;
}
}

/ p>

  var value is:-1 
我的内容是:myContent
detailContent是:我的第一行

而不是获取第三个参数的总内容(即detailContent),它只得到第一行。
任何建议获得完整的字符串内容。
提前Thansk。

解决方案

C和C ++中的变量参数不会像你认为的那样工作作品。有关如何使用变量参数的教程,请参见此链接。 / p>

或查看 stdarg 的手册页。


I have passed a printf formated string to a function and try to separate the content of that string by using sscanf(). But following is the problem with the use of this function. i have called the function as follws:

int var = -1;
ostringstream ossi;
ossi << "myContent";
std::string detailString = "my first line \n
                            the secon dline content is alos present \n
                             also we are also get s sdjhf sf"
myFunction("What value: %d \n  My content: %s \n Detail Content: %s", var, ossi.str().c_str(),  detailString.c_str());

The implementation is as follows:

void myFunction(const char * format, ...)
{

    int var;
    char myContent[10000];
    char detailContent[50000];
    if (sscanf(myString, "What value: %d \n My content: %s \n Detail Content: %s", &var, cmyContent, detailContent) == 3)
    {
      std::cout<<" #### "<<std::endl;
      std::cout<<"var value is :"<<var<<std::endl;
      std::cout<<"My content is :"<<myContent<<std::endl;
      std::cout<<"detailContent is :"<<detailContent<<std::endl;
      std::cout<<" #### "<<std::endl;
    }
}

with above function im getting out put as follows:

var value is : -1
My content is : myContent
detailContent is : my first line 

instead of getting total content of third argument(i.e detailContent) it gets only first line. any suggestion to get complete content of string. Thansk in advance.

解决方案

Variable arguments in C and C++ doesn't work the way you seem to think it works. See this link for a tutorial on how to use variable arguments.

Or check out the manual page for stdarg.

这篇关于如何获取字符串的总内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆