标签: stringstream

自定义类ostringstream输出错误

我不知道为什么这是错误的,但我只是想在endl中添加"akin",以便我可以将ostringstream中的内容添加到调试器中.我有以下内容:

class debug_stream_info
{
public:
    debug_stream_info(int errorLine, char *errorFile, int level)
        :m_errorLine(errorLine), m_errorFile(errorFile), m_logLevel(level)
    {
    }

    friend std::basic_ostringstream<char>& operator<<(std::basic_ostringstream<char>& os, debug_stream_info& debug_info);

private:
    int m_errorLine;
    std::string m_errorFile;
    int m_logLevel;

};

std::basic_ostringstream<char>& operator<<(std::basic_ostringstream<char>& os, debug_stream_info& debug_info)
{
    // Write the stream's contents to cpu_debug
    // Deleted custom logging function.  No errors here though

    // Clear the stream for re-use.
    os.str("");
    os.seekp(0);

    return os;
}

int main(int argc, char** argv)
{
    std::ostringstream myout;
    myout << "hey there" << " and some more …
Run Code Online (Sandbox Code Playgroud)

c++ stringstream

1
推荐指数
1
解决办法
1112
查看次数

C++ - stringstream <<"覆盖"

我正在用C++制作一个OpenGL游戏.与其他语言相比,我在C++方面相当无趣.无论如何,我为一些图像创建了一个带有"base"目录的字符串流.然后我将此stringstream作为函数参数传递给构造函数.构造函数附加图像文件名,然后尝试加载生成的路径.然而...

D:\CodeBlocks Projects\SnakeRoid\bin\Debug\Texts\ <-- before appending the filename
Ship01.tgacks Projects\SnakeRoid\bin\Debug\Texts\ <-- After.
Run Code Online (Sandbox Code Playgroud)

显然不正确!结果应为D:\ CodeBlocks Projects\SnakeRoid\bin\Debug\Texts\Ship01.tga

我的代码的相关部分:

std::stringstream concat;
std::string txtFullPath = "Path here";

...

concat.str(""); //Reset value (because it was changed in ...)
concat << texFullPath; //Restore the base path
PS = new PlayerShip(&TexMan, concat); //Call the constructor
Run Code Online (Sandbox Code Playgroud)

构造函数的代码

PlayerShip::PlayerShip(TextureManager * TexMan, std::stringstream &path)
{
    texId = 2;
    std::cout << path.str(); //First path above
    path << "Ship01.tga";
    std::cout  << path.str(); //Second - this is the messed up one
    //Do more …
Run Code Online (Sandbox Code Playgroud)

c++ std stringstream

1
推荐指数
1
解决办法
2866
查看次数

stringstream:为什么这段代码不返回4?

#include <iostream>
#include <sstream>

using namespace std;

int get_4()
{
  char c = '4';
  stringstream s(ios::in);
  s << c;
  int i;
  s >> i;
  return i;
}

int main()
{
  cout << get_4() << endl;
}
Run Code Online (Sandbox Code Playgroud)

转换对我不起作用.如果我将字符'4'或字符数组{'4','\ 0'}写入stringstream然后将其读出为int i,我就不会回复4.上述代码有什么问题?

c++ stringstream

1
推荐指数
1
解决办法
127
查看次数

流字符串流是libstdc ++扩展吗?

流式传输stringstreamlibstdc ++扩展?这个程序与编译gcc-4.2,gcc-4.7-2 (using -std=c++03)和铛3.2使用-std=c++11libstdc++(感谢安迪警车,见注释).它不clang 3.2使用-std=c++11和编译-stdlib=libc++.

#include<iostream>
#include<sstream>
int main() {

  std::stringstream s; s << "b";

  std::cout << "ss: " << s << std::endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

通过查看ofstream的构造函数,它可以采用a std::basic_streambuf<CharT, Traits>*或a basic_ostream& st.字符串流是一个std::basic_istream,但两者都是std::basic_ios<CharT, Traits>如此,我猜它应该工作.

以下更改使代码在clang下编译:

  std::cout << "ss: " << s.str() << std::endl;
Run Code Online (Sandbox Code Playgroud)

做正确的方法是什么?cout << s;还是cout << s.str();

c++ stringstream libstdc++ libc++

1
推荐指数
1
解决办法
544
查看次数

如何让std :: istringstream将给定的字符视为空格?

使用std::istringstream它很容易阅读由空白分隔的单词.但要解析以下行,我需要将字符/视为空格.

f 104/387/104 495/574/495 497/573/497
Run Code Online (Sandbox Code Playgroud)

如何读取由斜杠或空格分隔的值?

c++ parsing stringstream

1
推荐指数
1
解决办法
1317
查看次数

为什么stringstream :: str()截断字符串?

我有stringstream对象.它是通过填补

stringstream ss;
boost::iostreams::copy(inp,ss);
Run Code Online (Sandbox Code Playgroud)

boost::iostreams::filtering_streambuf<boost::iostreams::input> inp;
Run Code Online (Sandbox Code Playgroud)

并实际上保持在其中的ungzipped文件.

现在,如果我将stringstream内容刷新到文件

std::ofstream ofs(path_to_file,std::ios_base::out|std::ios_base::binary);
ofs << ss.rdbuf();
Run Code Online (Sandbox Code Playgroud)

一切都好.文件中包含完整的正确数据.

但是,如果我而不是像这样刷新文件构造字符串

std::string s = ss.str();
Run Code Online (Sandbox Code Playgroud)

内容在中间某处被截断.它不是持久性错误,它显然取决于字符串缓冲区的内容.

内容是几种语言的HTML文件.

它能是什么?谢谢.

c++ string stringstream boost-iostreams

1
推荐指数
1
解决办法
4327
查看次数

是否会出现问题stringstream.str().c_str()?

代码:

stringstream ss("012345678901234567890123456789012345678901234567890123456789");  
Run Code Online (Sandbox Code Playgroud)

一些文章说由于ss.str返回临时对象而导致后续使用是错误的,并且在调用之前会被破坏.c_str();

 const char* cstr2 = ss.str().c_str();  
Run Code Online (Sandbox Code Playgroud)

但我运行的例子,没有问题?如何理解?

c++ stringstream

1
推荐指数
3
解决办法
1万
查看次数

std:ostringstream和-std = c ++ 11的内存错误?

编辑:感谢所有指出问题的人,并在Stack Overflow上进行了讨论.我自己投了最后一次投票.

一个相关的问题:在没有CPP参考ostringstreamostringstream::str说明其暂时的.这么多人怎么知道的?或者我应该咨询不同的文件?


我有很多与在Debian 7.3(x64)的内存错误麻烦GCC 4.7.2,-std=c++11std::ostringstream.它导致bizaare结果,如/sf/ask/1488257081/.

完整的程序使用Sqlite.从命令行运行简化案例与Valgrind打印2个不同的错误.整个简化案例程序可以在Code Viewer中找到(我认为在这里发布整个示例有点长).它所做的只是初始化Sqlite,打开数据库,创建表,关闭数据库,以及将数据库统一化.如果发生错误,它会报告错误.没有其他事情发生.

这是简化案例程序的一部分,它只是试图创建一个表.如果表存在,它应该产生错误(它会):

ostringstream qs;
qs.str().reserve(96);

qs << "CREATE TABLE test";
qs << "(";
qs << "  userid INTEGER PRIMARY KEY AUTOINCREMENT,";
qs << "  username TEXT,";
qs << "  salt BLOB,";
qs << "  hmac BLOB";
qs << ");";

const char* stmt = qs.str().c_str();
AC_ASSERT(NULL != stmt);

rc = sqlite3_exec(db, stmt, NULL, NULL, &err);
AC_ASSERT(rc == SQLITE_OK);

if(rc != SQLITE_OK) …
Run Code Online (Sandbox Code Playgroud)

c++ memory-leaks stringstream temporary-objects c++11

1
推荐指数
2
解决办法
3351
查看次数

模板化运算符<<未被识别

我创建了一个名为SkipToChar的类,它应该能够按如下方式使用:

std::ostringstream oss;
oss << "Hello," << SkipToChar(7) << "world!" << std::endl;
Run Code Online (Sandbox Code Playgroud)

哪个会打印"Hello,world!" (注意空格.)基本上它应该使用空格跳转到指定索引处的字符.但显然编译器无法识别operator<<我为它创建的.有趣的是,呼吁operator<<明确,即使没有给予任何模板参数(如operator<<(oss, SkipToChar(7));工作正常;它只是不,如果我实际的工作

这是我的代码:

#include <iostream>
#include <sstream>

template <typename _Elem>
struct basic_SkipToChar
{
    typename std::basic_string<_Elem>::size_type pos;
    basic_SkipToChar(typename std::basic_string<_Elem>::size_type position)
    {
        pos = position;
    }
};

template <typename _Elem>
inline std::basic_ostringstream<_Elem> &operator<<(std::basic_ostringstream<_Elem> &oss, const basic_SkipToChar<_Elem> &skip)
{
    typename std::basic_string<_Elem>::size_type length = oss.str().length();
    for (typename std::basic_string<_Elem>::size_type i = length; i < skip.pos; i++) {
        oss << (_Elem)' ';
    }
    return oss;
}

typedef …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading operators stringstream

1
推荐指数
1
解决办法
43
查看次数

为什么std :: cout会在输出中添加return或换行符?

使用stringstream提取自句话.

每个单词都添加了换行符,我看不出原因; 你能?谢谢你的帮助.基思:^)

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>

int main() {
    int wordCount(9);
    std::vector<std::string> v(wordCount);
    std::vector<std::string>::iterator it;
    std::string tStr;
    std::string myStr = "the quick fox jumped over the lazy brown dogs";
    std::stringstream ss(myStr);
    std::cout << "Sentence broken into vector:" << std::endl;
    for (int i=0; i<wordCount; ++i) {
        ss >> tStr;
        v.push_back(tStr);
    }
    for (it=v.begin(); it != v.end(); ++it)
        std::cout << *it << std::endl;

    std::cout << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译,运行和输出.注意额外的换行符.

pickledEgg $ g++ …
Run Code Online (Sandbox Code Playgroud)

c++ vector stringstream

1
推荐指数
1
解决办法
184
查看次数