![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
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 |
|
|
|
|
|
|
#12 |
|
Newbie
Join Date: Feb 2006
Posts: 20
Rep Power: 0
![]() |
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" */
} |
|
|
|
|
|
#13 |
|
Hobbyist Programmer
|
not a problem now i dont even need it as the guy i was doing it for doesnt want it lol
|
|
|
|
|
|
#14 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#15 |
|
Hobbyist Programmer
|
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.
|
|
|
|
|
|
#16 |
|
Hobbyist Programmer
|
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. |
|
|
|
|
|
#17 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#18 |
|
Hobbyist Programmer
|
Im glad to, if the guy who asked me to do this decides he does want it well he aint gonna get it :p
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|