Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 9th, 2006, 5:41 AM   #11
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by DaWei
I don't say that it's better, Ruben. As a matter of fact, sprintf has been deprecated. Now ask yourself, if you were Hacker, and ALL of this was new to you, if you'd like to learn string streams at the same time as I/O and file streams, or if you'd maybe like to take one after the other. I guess I need to go back and read my posts, I don't recall saying it was the better option.
Sorry David. :o You said "Use string streams or sprintf."
Then Hacker says: "sprintf seems to be my best option but i have this code so far but it doesn't create the file"
So I say: "Why do you say that sprintf seems to be your best option?"

You still get it? I'm a bit now.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
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
Old Mar 9th, 2006, 9:08 AM   #13
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
not a problem now i dont even need it as the guy i was doing it for doesnt want it lol
HaCkeR is offline   Reply With Quote
Old Mar 9th, 2006, 9:30 AM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Wonderful. Makes me appreciate the 30 minutes I invested. Funny how the help drew out critical responses, too, when the only previous response directly to the OP was IR's.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 9th, 2006, 10:43 AM   #15
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
Don't get me wrong i still appericate the help and will still try to work it out im just saying its not desperatly needed now, it annoyed me alot when he turned around and said he don't need it. Also you seem to think i said that because of critical responses well no, ive always had critical responses in actual fact they can be very usful to me.
HaCkeR is offline   Reply With Quote
Old Mar 9th, 2006, 11:11 AM   #16
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
OK well thanks guys you all help in some way as ive cracked it now, althought i don't need it right now. Heres what i got. Ow i need to check for file opening but i can do that ive got the actual jo done so here it is

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
  stringstream ss;
  string fileName, baseName, baseExt;
  int i, num;
  fstream outPut;
  
  cout << "Enter File name: ";
  getline(cin, baseName);
  cout << "Enter File EXT: ";
  getline(cin, baseExt);
  cout << "Enter Amount Of File: ";
  cin >> num;
  
  while (num > 0)
  {
      num--;
      i++;          
      ss << baseName << i << baseExt << endl;
      getline(ss, fileName);
      outPut.open (fileName.c_str(), ios::binary | ios::out);
      outPut << "Hello";
      outPut.close ();
      cout << "File " << i << " created" << endl;
      }
  cout << "\n All files created thank you, press 'Enter' to exit" << endl;
  
  cin.sync();
  cin.get();
  return 0;
}

I added the same text to each file because thats what it was originally needed for.
HaCkeR is offline   Reply With Quote
Old Mar 9th, 2006, 11:22 AM   #17
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
glad you got it worked out, congrats.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Mar 9th, 2006, 11:29 AM   #18
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
Im glad to, if the guy who asked me to do this decides he does want it well he aint gonna get it :p
HaCkeR is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC