CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2007
    Posts
    51

    <stdio.h> and <iostream>

    Hi!

    I'm quite used to handling data with <iostream> library.

    In <stdio.h> there is a function called sprintf() that writes formatted data to a string. Does anyone know if there is a similar function in <iostream>?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: <stdio.h> and <iostream>

    There's stringstreams, which do the same thing, but the <iostream> library doesn't have a concept of format strings per se.

  3. #3
    Join Date
    Nov 2007
    Posts
    51

    Re: <stdio.h> and <iostream>

    OK thanks.

  4. #4
    Join Date
    Feb 2003
    Posts
    377

    Re: <stdio.h> and <iostream>

    You can format your output to any stream (whether it is an iostream or a stringstream) using manipulators from <iomanip>. For example, if you want your number output in hexadecimal with at least eight characters you might use:
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
        int val = 123456789;
        std::cout << "0x";
        std::cout << std::setw(8) << std::setfill('0');
        std::cout << std::hex << val << '\n';
    }
    // output:
    // 0x075bcd15

  5. #5
    Join Date
    Jul 2008
    Posts
    11

    Re: <stdio.h> and <iostream>

    Note that if you're using C++ and not C, you should include <cstdio> instead of <stdio.h>, because sometimes <stdio.h> might be non-standard.

  6. #6
    Join Date
    Feb 2003
    Posts
    377

    Re: <stdio.h> and <iostream>

    Quote Originally Posted by YAPP
    Note that if you're using C++ and not C, you should include <cstdio> instead of <stdio.h>, because sometimes <stdio.h> might be non-standard.
    Technically, <stdio.h> is standard in C++, so it will work on any standards-compliant compiler. It is deprecated, however, and you're right that you should use <cstdio> instead.

  7. #7
    Join Date
    Jul 2008
    Posts
    11

    Re: <stdio.h> and <iostream>

    Quote Originally Posted by Jpsarapuu
    Hi!

    I'm quite used to handling data with <iostream> library.

    In <stdio.h> there is a function called sprintf() that writes formatted data to a string. Does anyone know if there is a similar function in <iostream>?
    With regards to <stdio.h> and sprintf(), if you are not aware, you should be using snprintf() instead, as the 'n' means there is a parameter used to limit the max amount of characters written into the array. It's a more secure usage (though it doesn't prevent you from sticking in a MAX that is larger than your array).

    But I am not aware of a C++ equivalent.

    I'll take a stab at C++ suggestions: using setbuf() in <cstdio> to set up a buffer for writing re-direction, stringstream in <iostream>.

  8. #8
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: <stdio.h> and <iostream>

    You may find this article on the topic helpful. It compares all the standard ways of converting numbers to strings, and gives advanatges and disadvantages of each.
    Old Unix programmers never die, they just mv to /dev/null

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured