can any one tell me how to clear the content of the stringstream..? i tried the following but it didnt work.  stringstream ss; ss&lt;&lt;"bala"&lt;&lt;"murugan"; cout&lt;&lt;ss.str();         //Output balamurugan ss.str().clear(); ss&lt;&lt;" hi"; cout&lt;&lt;ss.str(); // output is balamurugan hi    my required output is " hi" alone.. Pls tell me
42244
I've tried several things already,  std::stringstream m; m.empty(); m.clear();   both of which don't work.
37382
I'm a C++ guy learning Java. I'm reading Effective Java and something confused me. It says never to write code like this:  String s = new String("silly");   Because it creates unnecessary String objects. But instead it should be written like this:  String s = "No longer silly";   Ok fine so far...However, given this class:  public final class CaseInsensitiveString {     private String s;     public CaseInsensitiveString(String s) {         if (s == null) {             throw new NullPointerException();         }         this.s = s;     }     :     : }  CaseInsensitiveString cis = new CaseInsensitiveString("Polish"); String s = "polish";    Why is the first statement ok? Shouldn't it be   CaseInsensitiveString cis = "Polish"; How do I make CaseInsensitiveString behave like String so the above statement is OK (with and without extending String)? What is it about String that makes it OK to just be able to pass it a literal like that? From my understanding there is no "copy constructor" concept in Java?
57883
I've just come across the &lt;&lt; command, used like so:  cat &gt; outfile.txt &lt;&lt;EOF Multi-line content that will be written to outfile.txt EOF   Now, I've no idea what this is called, but I'd quite like to know it's name, primarily so I can go and search and find out more about its syntax. Sadly, Googling for "&lt;&lt;" just doesn't work.
73774
What's the best way to pipe the output from an java.io.OutputStream to a String in Java?  Say I have the method:    writeToStream(Object o, OutputStream out)   Which writes certain data from the object to the given stream. However, I want to get this output into a String as easily as possible.  I'm considering writing a class like this (untested):  class StringOutputStream extends OutputStream {    StringBuilder mBuf;    public void write(int byte) throws IOException {     mBuf.append((char) byte);   }    public String getString() {     return mBuf.toString();   } }   But is there a better way? I only want to run a test!
19556
Yes,  but there must be a canonical or most efficient or most concise way. I'll add answers I know of and see what percolates to the top.  To be clear, the question is how best to read the contents of a file into a string. One solution per answer.
49432
I'm working in a C++ unmanaged project.  I need to know how can I take a string like this "some data to encrypt" and get a byte[] array which I'm gonna use as the source for Encrypt.  In C# I do    for (int i = 0; i &lt; text.Length; i++)     buffer[i] = (byte)text[i];   What I need to know is how to do the same but using unmanaged C++.  Thanks!
121685
How to fetch HTML in C or C++? with Sockets. Can you give me a Example code pls?  NOTE - I originally closed this, but reopened.  To the poster - Please see:    and
26743
How can I format my output in C++? In other words, what is the C++ equivalent to the use of printf like this:  printf("%05d", zipCode);   I know I could just use printf in C++, but I would prefer the output operator &lt;&lt;.  Would you just use the following?  std::cout &lt;&lt; "ZIP code: " &lt;&lt; sprintf("%05d", zipCode) &lt;&lt; std::endl;
3852
Is a string literal in c++ created in static memory and destroyed only when the program exits?
46309
I just wrote the following C++ function to programatically determine how much RAM a system has installed.  It works, but it seems to me that there should be a simpler way to do this.  Can someone tell me if I'm missing something?  getRAM() {     FILE* stream = popen( "head -n1 /proc/meminfo", "r" );     std::ostringstream output;     int bufsize = 128;      while( !feof( stream ) &amp;&amp; !ferror( stream ))     {         char buf[bufsize];         int bytesRead = fread( buf, 1, bufsize, stream );         output.write( buf, bytesRead );     }     std::string result = output.str();      std::string label, ram;     std::istringstream iss(result);     iss &gt;&gt; label;     iss &gt;&gt; ram;      return ram; }   First, I'm using popen("head -n1 /proc/meminfo") to get the first line of the meminfo file from the system.  The output of that command looks like     MemTotal:       775280 kB   Once I've got that output in an istringstream, it's simple to tokenize it to get at the information I want.  My question is, is there a simpler way to read in the output of this command?  Is there a standard C++ library call to read in the amount of system RAM?
31467
How to copy the contents of a file in UNIX without displaying the file contents. I don't want to cat or vi to see the contents.  I want to copy them to clipboard so that I can paste it back on my windows notepad.  I can't copy the file from that server to another due to access restrictions.
2376
I was surprised to find today that I couldn't track down any simple way to write the contents of an InputStream to an OutputStream in Java. Obviously, the byte buffer code isn't difficult to write, but I suspect I'm just missing something which would make my life easier (and the code clearer).  So, given an InputStream in and an OutputStream out, is there a simpler way to write the following?  byte[] buffer = new byte[1024]; int len = in.read(buffer); while (len != -1) {     out.write(buffer, 0, len);     len = in.read(buffer); }
2292
I have been working with a string[] array in C# that gets returned from a function call. I could possibly cast to a Generic collection, but I was wondering if there was a better way to do it, possibly by using a temp array.  What is the best way to remove duplicates from a C# array?
106520
I am trying to deserialize a stream but I always get this error "End of Stream encountered before parsing was completed"?  Here is the code:          //Some code here         BinaryFormatter b = new BinaryFormatter();         return (myObject)b.Deserialize(s);//s---&gt; is a Stream object that has been fill up with data some line over here   Any one have ideas?
78827
My brother recently started learning C++. He told me a problem he encountered while trying to validate input in a simple program. He had a text menu where the user entered an integer choice, if they entered an invalid choice, they would be asked to enter it again (do while loop). However, if the user entered a string instead of an int, the code would break. I read various questions on stackoverflow and told him to rewrite his code along the lines of:    #include&lt;iostream&gt; using namespace std;  int main() {     int a;     do     {     cout&lt;&lt;"\nEnter a number:"     cin&gt;&gt;a;         if(cin.fail())         {             //Clear the fail state.             cin.clear();             //Ignore the rest of the wrong user input, till the end of the line.             cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(),\                                                     '\n');         }     }while(true);     return 0; }   While this worked ok, I also tried a few other ideas: 1. Using a try catch block. It didn't work. I think this is because an exception is not raised due to bad input. 2. I tried if(! cin){//Do Something} which didn't work either. I haven't yet figured this one out. 3. Thirdly, I tried inputting a fixed length string and then parsing it. I would use atoi(). Is this standards compliant and portable? Should I write my own parsing function? 4. If write a class that uses cin, but dynamically does this kind of error detection, perhaps by determining the type of the input variable at runtime, would it have too much overhead? Is it even possible?  I would like to know what is the best way to do this kind of checking, what are the best practices?  I would like to add that while I am not new to writing C++ code, I am new to writing good standards compliant code. I am trying to unlearn bad practices and learn the right ones. I would be much obliged if answerers give a detailed explanation.  EDIT: I see that litb has answered one of my previous edits. I'll post that code here for reference.  #include&lt;iostream&gt; using namespace std;  int main() {     int a;     bool inputCompletionFlag = true;     do     {     cout&lt;&lt;"\nEnter a number:"     cin&gt;&gt;a;         if(cin.fail())         {             //Clear the fail state.             cin.clear();             //Ignore the rest of the wrong user input, till the end of the line.             cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(),\                                                     '\n');         }         else         {             inputCompletionFlag = false;         }     }while(!inputCompletionFlag);     return 0; }   This code fails on input like "1asdsdf". I didn't know how to fix it but litb has posted a great answer. :)
47388
In C#, can I convert a string value to a string literal, the way I would see it in code? I would like to replace tabs, newlines, etc. with their escape sequences. If this code: Console.WriteLine(someString);  produces: Hello World!  I want this code: Console.WriteLine(ToLiteral(someString));  to produce: \tHello\r\n\tWorld!\r\n
67482
What is the longest string that can be created in .NET? The docs for the String class are silent on this question as far as I can see, so an authoritative answer might require some knowledge of internals. Would the maximum change on a 64-bit system?  [This is asked more for curiosity than for practical use - I don't intend to create any code that uses gigantic strings!]