View Single Post
Old Mar 9th, 2006, 6:02 AM   #12
Bench
Newbie
 
Join Date: Feb 2006
Posts: 20
Rep Power: 0 Bench is on a distinguished road
I believe he's referring to the fact that your examples encouraged sprintf() over streams, not necessarily a claim that you said "X is better than Y".
On the other hand, he may have a point - The basic usage of stringstreams isn't really any harder than iostreams or fstreams - and it can be a helpful revelation to the learner, realising that all streams are fundamentally the same (Wheras the average beginner thinks in terms of "strings", "files" and "console i/o" and generally treats them as irreconcilable concepts)
 /* Just a snippet for the OP - same basic method as DaWei, but 
    with streams as opposed to sprintf */

std::string fileName;
std::stringstream ss;

for (unsigned i=0; i < desiredFiles; ++i)
{
   ss << baseName << i << baseExt << std::endl;
      /* "ss" works just like "cin" or "cout" - but instead of passing the data 
         directly to the console, the data is stored inside the stream. */

   std::getline(ss, fileName);
      /* Now fileName contains the full name of the file, eg myfile1.txt
         using exactly the same method as you would with "cin" */

   myFile.open (fileName.c_str(), ios::binary | ios::out);
   if (!myFile.good ()) return uhOh ("Ouput file didn't open");
   myFile << "This is file " << i << endl;
   myFile.close ();
   myFile.open (fileName.c_str(), ios::binary || ios::in);
   if (!myFile.good ()) return uhOh ("Input file didn't open");
   cout << myFile.rdbuf ();
   myFile.close ();
      /* This code is exactly the same as DaWei's - I just copy & pasted it
         The only difference is the ".c_str()" after "fileName" */
}
Bench is offline   Reply With Quote