Format string in C++

This is a discussion on Format string in C++ within the C++ Programming forums, part of the General Programming Boards category; Hello, I would like to know how to format strings in C++ like the string.Format from C#. I think sprintf ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Kaiserslautern
    Posts
    5

    Format string in C++

    Hello,

    I would like to know how to format strings in C++ like the string.Format from C#. I think sprintf it is only usable with chars, which is not the case. I tried boost with the following code:

    Code:
    const std::string ProcInfoParser::statmPath = "/proc/%1%/statm";
    
    void ProcInfoParser::gatherMemoryUsage(int _processPid) {
    
    
        std::string path;
    
        cout << boost::format("/proc/%1%/statm") % _processPid;
    
    
        std::string data;
        data = parseData(path);
    
    
        return;
    }
    But no success, as I got the following errors:


    Code:
    ../src/parser/ProcInfoParser.cpp: In member function ‘void ProcInfoParser::gatherMemoryUsage(int)’:
    ../src/parser/ProcInfoParser.cpp:32: error: no matching function for call to ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >::append(boost::basic_format<char, std::char_traits<char>, std::allocator<char> >&)’
    /usr/include/c++/4.4/bits/basic_string.tcc:325: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::append(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
    /usr/include/c++/4.4/bits/basic_string.tcc:342: note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::append(const std::basic_string<_CharT, _Traits, _Alloc>&, typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
    /usr/include/c++/4.4/bits/basic_string.tcc:298: note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::append(const _CharT*, typename _Alloc::rebind<_CharT>::other::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
    /usr/include/c++/4.4/bits/basic_string.h:863: note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::append(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
    /usr/include/c++/4.4/bits/basic_string.tcc:281: note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::append(typename _Alloc::rebind<_CharT>::other::size_type, _CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
    make: *** [src/parser/ProcInfoParser.o] Error 1
    How should I proceed?

    Pedro

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    Quote Originally Posted by pmdusso
    I tried boost with the following code:
    Code:
    const std::string ProcInfoParser::statmPath = "/proc/%1%/statm";
     
    void ProcInfoParser::gatherMemoryUsage(int _processPid) {
     
     
        std::string path;
     
        cout << boost::format("/proc/%1%/statm") % _processPid;
     
     
        std::string data;
        data = parseData(path);
     
     
        return;
    }
    Post the smallest and simplest compilable program that demonstrates the error. For example, this compiles without even a warning for me:
    Code:
    #include <iostream>
    #include <boost/format.hpp>
    
    void gatherMemoryUsage(int processPid)
    {
        std::cout << boost::format("/proc/%1%/statm") % processPid;
    }
    
    int main()
    {
        gatherMemoryUsage(1);
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Kaiserslautern
    Posts
    5
    Ok, your exemple also compiles for me. But the following no:

    Code:
    #include <iostream>
    #include <boost/format.hpp>
    
    
    void gatherMemoryUsage(int processPid)
    {
    	std::string a;
        a << boost::format("/proc/%1%/statm") % processPid;
    }
    
    
    int main()
    {
        gatherMemoryUsage(1);
    }
    The problem is in the assignment of the string. How does it work in c++? I tried using string.append but it also does not work, and string << boost:: [...] also does not.

    Thanks!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    Quote Originally Posted by pmdusso
    The problem is in the assignment of the string. How does it work in c++? I tried using string.append but it also does not work, and string << boost:: [...] also does not.
    This is a matter of reading and understanding the Boost.Format documentation. For example:
    Code:
    #include <iostream>
    #include <string>
    #include <boost/format.hpp>
    
    void gatherMemoryUsage(int processPid)
    {
        std::string a(str(boost::format("/proc/%1%/statm") % processPid));
        std::cout << a << std::endl;
    }
    
    int main()
    {
        gatherMemoryUsage(1);
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,511
    you can use a std::stringstream for this. it works exactly like cin/cout, except it stores its data in a string, instead of reading/writing the console.

    Code:
    #include <iostream>
    #include <sstream>
    #include <boost/format.hpp>
     
     
    void gatherMemoryUsage(int processPid)
    {
        std::stringstream ss;
        ss << boost::format("/proc/%1%/statm") % processPid;
        std::string a = ss.str();
    }
     
     
    int main()
    {
        gatherMemoryUsage(1);
    }

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,823
    You can also do:
    Code:
    #include <iostream>
    #include <string>
    #include <boost/format.hpp>
     
    void gatherMemoryUsage(int processPid)
    {
        std::cout << (boost::format("/proc/%1%/statm") % processPid).str() << std::endl;
    }
     
    int main()
    {
        gatherMemoryUsage(1);
    }
    Don't forget to include <string>!
    Also consider that what boost::format returns is not something that is directly printable AFAIK. You can get a std::string with the formatted string by calling the .str() function of the resulting boost::format returned object.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Validation of string format
    By Evenstevens in forum C Programming
    Replies: 8
    Last Post: 05-03-2009, 05:04 AM
  2. string.writeline format
    By George2 in forum C# Programming
    Replies: 3
    Last Post: 07-31-2008, 02:41 PM
  3. Format String Attack
    By tezcatlipooca in forum C Programming
    Replies: 16
    Last Post: 06-11-2007, 12:16 PM
  4. string format ...
    By SkinneyEd in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2005, 10:08 AM
  5. CGI String Format
    By sean in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-12-2002, 06:11 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21