Text File(2)

Okay, so I have a quick question about text files. When making one (myfile.open ("textfile.txt")) how do I make it possible to make multiple copies? As in, when the program makes a file that already has been named, it instead makes a new file called textfile(1).txt, or textfile(2).txt, and on and on after you repeat it the first time. If you need a better explanation, leave a comment.
I need further explanation.
Well you would have to open multiple file streams:
1
2
3
4
5
std::ofstream myfile, myfile1, myfile2;

myfile.open("textfile.txt");
myfile1.open("textfile(1).txt");
myfile2.open("textfile(2).txt");


Then you would have to write to every one:
1
2
3
myfile << "message";
myfile1 << "message";
myfile2 << "message";


You could then, of course, automate this procedure inside a function. Better still you could encapsulate the ofstreams in a class with suitable << operator overloads (don't worry if this second option is to complicated for you at this time, although there are relevant tutorials on this site - see "classes" to get started).
Uhm, better explanation...

You know, how when you start to play a game, and you save it, the save file goes under 'save1', 'save2', 'save3' and on and on? Well I want to know how to recreate that. I think Xander213 has the right idea here, and I would just check to see if the files were filled already, or not, and then make about 100 :P. Or just have a +(1), +(2), kinda thing, IDK. I guess I'll just have to make up something, but thanks for the tip Xander! :-)
Oh, I misunderstood. I thought you wanted to write the same thing to multiple files. I know what you mean now. So you want to check if a file exists, right? Consider this code:
1
2
3
4
5
6
7
8
9
10
11
std::ifstream test("textfile.txt");
if (test.fail())
{
   // something went wrong
   // there could be some other issue, but with luck this means the file does not exist
}
else
{
   // the file opened successfully
   // this means it exists, so you must choose a new filename
}


As for appending numbers to a filename, just use stringstreams:
http://cplusplus.com/reference/iostream/ostringstream/
(remember to include the <sstream> header).

Hope this helps.
Tons. Thank you Xander, now teach me how to make a Windows Application xD Haha, oh I'm so screwed. I tried installing SDML, or whatever, 'Simple DirectMedia Layer'. Won't install correctly, or it did and I just don't know how to start it up =P. Would be nice if someone wrote a tutorial from start to finish on making a game with Dev C++... =D

In any case, thanks for all the help, if you can fix my problem with SDL, please send me a post or message, and maybe someday, we can create a game! =D
Lol - you may be confusing SDL and SFML ;) I am writing a tutorial start to finish for a game (engine) in SFML (http://sfmlcoder.blogspot.com/ ) and the official website covers use of the library in a lot of detail (http://sfml-dev.org). For SDL there are loads of tutorials on http://www.sdltutorials.com. For general game making, I have previously used http://www.gpwiki.org/.

What exactly was the problem you had with setting up? Perhaps we can help.

PS: DevC++ is no longer supported, and it's compiler is out of date. If you can face it, it might be worth switching to another IDE such as Code::Blocks, Visual C++ or Netbeans.

Hope this helps.
Last edited on
I switched to Visual like 12 hours before ou made that post xD

Also, thanks for the links, I'm checkint them out now. BTW, your tutorial link was broken :'( I really wanna see it.

My problem with the start-up is just the basics. I mean, as soon as I can figure out the so called 'basic differences' from making a console to making a window, I may be able to self teach myself with different methods. I would prefer, however, a very very basic game program, with small graphics and videos (I.E. a picture for starting, a starting video, and a picture while you play), and a tutorial behind it saying what does what.

Like... it'lll show the code for the game. And a little message after every part:

WinMain//This is the equal to int main

kinda like that ^

And show how something is submitted into the game when a person opens up the application or ect. For example:

cin>>//This allows the user to enter in info, acronym for Code In

To me, something with complete examples would be the PERFECT tutorial, and then have a Q&A at the bottom and post every question and answer (in short form) on the sides of the tutorial so people who get those questions right away will seem them next to the problem area...

example:

FaQ: cout uses '<<' and cin uses '>>' cin>>//This allows the user to enter in info, acronym for Code In FaQ: There are more than one way to get code in from the user, please read further into the tutorial...


And, like I said, this tutorial is my opinion of a great one, so it suits me the best. I'm sure others would want a different type of tutorial.
I switched to Visual like 12 hours before ou made that post xD

A good decision :D

I fixed my link - the bracket had been absorbed into the link which was why it wasn't working... However, to be honest, there's not so much on the blog yet. However, if you want an introduction then this post shows you how to make a sample application - and there's a demonstration video :D
http://sfmlcoder.blogspot.com/2011/03/first-steps_31.html

Also, if you're unsure of how to setup SFML 2.0 in Microsoft Visual C++ Express then this post may help:
http://sfmlcoder.blogspot.com/2011/03/first-steps.html

Oh, and if you're still interested in this area in a month or two, then you might want to check out my new blog at http://sfmlcoder.wordpress.com/. When I've got that up and running I'll be making video tutorials as well :)

WinMain//This is the equal to int main

With SFML, you can just set your application as a console app, in which case your entry point is int main() as normal. If you make a windows app and use it with SFML, you can just link "sfml-main.lib" in release config and "sfml-main-d.lib" in debug config and then still use "int main()" as an entry point.

Hope this helps.
1
2
3
4
int mind
string Fucked

mind == Fucked


!!Error!!

No, I'm just kidding. Thanks for all the help Xander, I'm to tired to go link hunting but I'll be a major fan of your tutorials =D, maybe someday we'll invent the next Star Wars games (in terms of fame and income) xD
Topic archived. No new replies allowed.