Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] Putting data into a string
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Mon Oct 04, 2004 5:54 pm   Post subject: [Tutorial] Putting data into a string

Yesterday I wrote a tutorial on extracting data from a string using the istringstream class. Today I'll cover putting data into a string using the ostringstream class.

code:
#include <string>
#include <sstream>
#include <iostream>

int main()
{
   std::ostringstream output;
   int i = 37;
   double d = 27.42;

   output << i << d;

   std::string final_string = output.str();

   std::cout << final_string << std::endl;

   return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Mon Oct 04, 2004 9:36 pm   Post subject: (No subject)

thats rather cool. so ehm.. would u suggest using this over sprintf/sscanf ?
wtd




PostPosted: Mon Oct 04, 2004 10:11 pm   Post subject: (No subject)

If you're writing C code, write C (sprintf, sscanf, etc). If you're writing C++, then write C++. Smile
rizzix




PostPosted: Mon Oct 04, 2004 10:20 pm   Post subject: (No subject)

ha well .. but i find it easier to do error checking using stdio than using iostreams.

for ex:
code:

int num;
char* str = "1000";

if (!sscanf(str, "%d", &i))
    printf("error\n");



and the syntax is a lot cleaner.. no operator overloading Razz .. although that shouldn't bug a c++ programmer a lot.. i rather like a consistent behaviour. and the c libraries tend to provide that.

but things would be different if iostream supported exceptions.
wtd




PostPosted: Tue Oct 05, 2004 12:23 am   Post subject: (No subject)

The problem with stdio.h is the danger of buffer overflows.
Tony




PostPosted: Wed Oct 06, 2004 4:15 pm   Post subject: (No subject)

i seem to be having trouble figuring out how to flush ostringstream Confused
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: