|
This thread is locked; no one can reply to it. |
1
2
|
Arrays to files? |
Durnus
Member #7,997
November 2006
|
I have an array, how would I save it to a file? All I need is the ability to save it from the program and load it later. I am using C++, so I would prefer a C++ function to do this.:P
|
piccolo
Member #3,163
January 2003
|
edit: the header file is #include <fstream.h>
edit: this is what the file looks like 7, if it is unclear please tell me. wow |
Indeterminatus
Member #737
November 2000
|
If you can afford the dependency on Boost, it has a very neat serialization module. You can read up on it here. It works very nicely with STL, so if your array is in fact stored in a std::vector or a std::list, saving and loading the entire thing requires very little effort. _______________________________ |
TeamTerradactyl
Member #7,733
September 2006
|
piccolo: I certainly hope you didn't post peoples' passwords in that list!
|
piccolo
Member #3,163
January 2003
|
no those are test user accounts the password for all those users is password. but i disabled password input so it will be essayer to test every thing. wow |
Matthew Dalrymple
Member #7,922
October 2006
|
That's kind of a security risk, posting passwords in plain text... md5 them before you save them to the file? =-----===-----===-----= |
piccolo
Member #3,163
January 2003
|
yeah i know but this is still in developing faze so its a lot easer to debug when you can read whats in the text file. wow |
HardTranceFan
Member #7,317
June 2006
|
Durnus, I'm not sure if it'll it'll work with arrays, but using ofstream.write (google on fstream) you can output a class: #include <fstream> ... ofStream fout("someFileName", ios::binary); fout.write((char*) &class, sizeof class); fout.close(); ... ifstream fin("someFileName", ios::binary); fin.read((char*)&class, sizeof class); fin.close(); ...
-- |
TeamTerradactyl
Member #7,733
September 2006
|
Durnus, could you give a little more detail for clarification? As piccolo's example showed, the first line of text you output states "how many lines/records will follow", and you would loop that many times when you decide to read back the data. Something like:
You would have to have "yourfile.txt" in that specific order, and prefixed with the number of records you want to read in. Another method would be to save each record on a separate line, and consider the newline as a record-delimiter (meaning you wouldn't have to know before-hand how many records you had, but you WOULD need some sort of dynamic array to store them into: like std::vector). I guess my question is, you have "an array" of something. Are you asking how to output to a file, or how to read in your array data from the disk after you're already saved it?
|
BAF
Member #2,981
December 2002
|
You still aren't using a database? sqlite would work perfect for you in this case. |
piccolo
Member #3,163
January 2003
|
yeah i'm am but i want to be the first mmrpg in the depot where someone can compile the code. if i put the 3rd party database that will mean in or to try my code you will have to have Oracla 10g installed. im going to make 2 versions of the game one with oracle and one with out. i did do the research and i got have a data base running on my computer. and i got it to work with c++ usint the ODB class library but ill save that for the other version of the game. wow |
Jonatan Hedborg
Member #4,886
July 2004
|
Quote: yeah i'm am but i want to be the first mmrpg in the depot where some can compile the code. if i put the 3rd party database that will mean in or to try my code you will have to have Oracla 10g installed. im going to make to version of the game one with oracle and one with out. i did do the research and i got have a data base running on my computer. and i got it to work with c++ use ODB class library but ill save that for the other version of the game.
No offense but; Did you even try to write in English?
|
Thomas Fjellstrom
Member #476
June 2000
|
You don't add ANY files to your project to use it. Just #include the sqlite header once you have sqlite installed. -- |
Jonatan Hedborg
Member #4,886
July 2004
|
Right you are, i just checked. Just compile it and link the static library. It's also very easy to use (although I'm not that crazy about the function-pointer dependent execute function). And there are some nice, free, sqlite "browsers" out there.
|
Durnus
Member #7,997
November 2006
|
Sorry about not responding to my post, but I can't use a computer during the week. I don't really need specific code, but just a way to write to a file (a text file or whatever) and put it back into my program. More specifically, what I need is a function to output to a file, (I am seeing this as outData in the examples.) and a function to get data from a file. (inData?) I also need to be able to tell when a new line starts. And yes, I have already planned to use a vector to store my array on input, and to put it into a file I will iterate through my array and print the data in array-like form.
|
Michael Jensen
Member #2,870
October 2002
|
all you need is: or if using allegro I suggest using pack_fopen, pack_fwrite, and pack_fread the allegro routines can be setup to use transparent compression. -- Offtopic: -- Don't use oracle as a db server for a game, use sqlite, or if you absolutely have to mysql (mysql is free and can be setup anywhere, oracle is not) -- as a side option there is always SQL Server, the 2005 version is free but only supports one connection at a time or something along those lines... anyway lots of web hosts will sell you access to sql server 2000/2005 for under $10/mo ... -- sorry to butt in if I'm misunderstanding.
|
TeamTerradactyl
Member #7,733
September 2006
|
Durnus, are you asking to open the array into a file using the Allegro library, or just "in general" using iostreams?
|
BAF
Member #2,981
December 2002
|
Quote: anyway lots of web hosts will sell you access to sql server 2000/2005 for under $10/mo What webhosts do that? Most hosts, in most cases, if they are smart, only allow access to their SQL servers from other servers that they run. A SQL server open to the public is usually a big security issue, especially if it's a Microsoft product. But, I do agree that sqlite is the best idea here. But anyway, if you are planning on running an open SQL server for the release version of your game you have serious design flaws. The clients shouldn't even know about SQL (unless they use sqlite for local storage). All processing and such should be done by the server and stored server side on to private SQL storage. |
Michael Jensen
Member #2,870
October 2002
|
I didn't say direct access -- I've actually not done it -- but my host offers it, I'm not sure HOW you access it, and the only server that has direct access to it is the web server -- I probably have to login to some web interface that lets me run DB scripts / manage my own DB's permissions -- and I'm pretty sure that the $10 is per DB... -- However my host also offers access to mysql databases... I wonder if they charge for it? probably. Back on topic, yeah: no, the client should never have access to the database, the database should be server side -- sqlite is light, easy, and fast -- it's also file based instead of client/server based (sql server/mysql both require TCP connections, even if it's thru local host...) mysql/sql server hosting with your game hosting is going to cost a pretty penny.
|
piccolo
Member #3,163
January 2003
|
oracle is free as long as its not used for comurshell purpuses. plus using it in your games is a good why to learn it for the work place. wow |
Michael Jensen
Member #2,870
October 2002
|
it's really not suited for game development though -- again it's a server database engine -- I suppose if whoever is hosting your game is also hosting an oracle server then why not? -- but it's going to be slower than SQLite, larger, and harder to work with... also Oracle seems like it's getting obscure anymore -- the only places that I've worked that use Oracle, use Orcale because they've been using it 15 to 20 years...
|
Durnus
Member #7,997
November 2006
|
TeamTerradactyl said: Durnus, are you asking to open the array into a file using the Allegro library, or just "in general" using iostreams? Good question. All I'm asking is to be able to write into a file and put the file into a variable. (EDIT: ) I'm not looking to use the allegro library, I'm actually hoping to use some standard library, such as fstream. Now I know how to write into a file, but I still don't know how to read a file into a variable. All the websites I look at aren't very clear about reading.
|
BAF
Member #2,981
December 2002
|
BTW, HardTranceFan, that is a horrible practice which should not be practiced (writing whole classes/structs to files). Anyway, Durnus, did you read this post? |
TeamTerradactyl
Member #7,733
September 2006
|
Durnus said: Now I know how to write into a file, but I still don't know how to read a file into a variable. All the websites I look at aren't very clear about reading. Durnus, if you are already using fstream to write OUT to a file, you can use the same function to read it back in. (Some example code is in this same thread: http://www.allegro.cc/forums/thread/588899/633084#target ) Might I also suggest: If you've ever used "cin" or "cout" from the console for a simple "Hello World" program, you can use fstream in almost the exact same way: I usually name my "file input" and "file output" as "fin" and "fout", to match the "console (in|out)" of "(cin|cout)":
I hope this helps a bit: You OUTPUT to the screen or file with "<<", like cout << "Hello, world!" and read INPUT from a keyboard (console input device) or file with ">>", like cin >> userResponse; or fin >> someDataFromFile;. I hope this helps!
|
Durnus
Member #7,997
November 2006
|
Yes, it helps, but I just need one more thing. How does the inputting know where to stop putting the data into a variable? Does it stop when it hits a space? Does it stop when it hits a new line? Can I set what it stops at? From what I've seen, the input function keeps on incrementing itself, and that was a question I had. Anyway, thanks for all the help.
|
|
1
2
|