Page 1 of 2 1 2 LastLast
Results 1 to 20 of 36

Thread: C++ Question     submit to reddit submit to twitter

  1. #1
    Relic Weapons
    Join Date
    Jan 2006
    Posts
    334
    BG Level
    4

    C++ Question

    Hey, I've been messing around with some c++ programs I wrote back in highschool to try and reteach myself everything i've forgotten since then.

    One thing we never covered and I'd like to learn how to do is saving certain variables in such a way that upon opening the program I can call them and basically 'load' from where I left off last time.

    I can clarify what I mean if you need, but does anyone know how to do something like this or where to find a tutorial on it?

  2. #2
    Relic Weapons
    Join Date
    Apr 2006
    Posts
    312
    BG Level
    4

    My C++ is a bit rusty, but I'd think the easiest way to do this would just be to output all the variables you'd want to change to a text file, that your program reads in and interprets each time it starts up.

  3. #3
    Relic Weapons
    Join Date
    Jan 2006
    Posts
    334
    BG Level
    4

    I honestly only know some basic stuff about c++. . . so I can think of some ways to do it, but I don't know how to do it, ya know?

    If someone could show me an example or something similar that I can work off of it'd be fantastic

  4. #4
    Chram
    Join Date
    Jun 2006
    Posts
    2,539
    BG Level
    7

    Someone apparently needs help with their homework.

  5. #5
    E. Body
    Join Date
    Jun 2006
    Posts
    2,181
    BG Level
    7
    FFXIV Character
    Bro Teampill
    FFXIV Server
    Gilgamesh
    FFXI Server
    Ifrit

    Pretty much what he said. The easiest way is to save the state of your variables to a settings store. Either the registry, or the preferred way of doing it via saving an ini/xml or similiar file to the current user's application data folder.

    Assuming you're using Windows, some of the API's to look up:

    Registry:
    RegOpenKeyEx
    RegCreateKeyEx
    RegCloseKey
    RegEnumKey
    RegQueryValueEx
    RegCloseKey
    RegSetValueEx

    XML:
    IXMLDOMDocument2
    IXMLDOMNodePtr
    IXMLDOMElementPtr
    IXMLDOMAttributePtr

    File IO:
    CreateFile
    WriteFile
    CloseHandle

    Personally I prefer using XML because it's such a flexible and robust method of munging data, especially if there's even the most remote possibility that its format will change.

  6. #6
    E. Body
    Join Date
    Jun 2006
    Posts
    2,181
    BG Level
    7
    FFXIV Character
    Bro Teampill
    FFXIV Server
    Gilgamesh
    FFXI Server
    Ifrit

    Oh and just a quick and dirty file write sample.


    Code:
    #include <windows.h>
    #include <stdio.h>
    
    // Random data structure (your own state vars would be here instead)
    struct MYFILEDATA
    {
       int nSomeData;
       BOOL bIsActive;
       char data[20];
    }
    
    MYFILEDATA data; 
    DWORD dwWrite = 0;
    
    // Fill in some values for the hell of it.
    data.nSomeData = 1234;
    data.bIsActive = TRUE;
    for(int i=0;i<20;i++){data.data[i]=i;}
    
    // Create a file, using the flag to always create it, even if a file already exists of the same name.
    HANDLE hFile = CreateFile("settings.dat", GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, 0, NULL);
    if(INVALID_HANDLE == hfile) // Make sure the handle to the file is valid.
    {
       // Something went wrong, maybe the file is read-only or some crap. Return E_FAIL so the app can take appropriate action.
       return E_FAIL;
    }
    
    // Call WriteFile, passing the handle to the file, the address of the data to write out, and the size of the data to write in bytes,
    // also passing in the address of dwWrite that WriteFile will fill in with the number of bytes written.
    WriteFile(hFile, &data, sizeof(MYFILEDATA), &dwWrite, NULL);
    if(sizeof(MYFILEDATA) != dwWrite) // Make sure that the number of bytes that dwWrite contains matches the size of the data that was attempted to be written.
    {
       CloseHandle(hFile); // Close the handle before returning.
       return E_FAIL; // Failed to write, because dwData didn't match the size of the data. Maybe the disk was full or an I/O error occurred.
    }
    
    CloseHandle(hFile); // All done, close the file handle.
    Keep in mind I didn't compile this so it may have errors, just typing off the top of my head. It just gives the jist of writing a simple struct to a file.

  7. #7
    Relic Weapons
    Join Date
    Jan 2006
    Posts
    334
    BG Level
    4

    I am using windows, yes. I took a look at the code posted and it's very confusing

    I'm using the borland c++ package which has stdio.h and windows.h so with the correct coding that method should work, but I'm not sure exactly what's going on in there. Is there any way you could comment it to explain it a little more or point me to an explination of what you're doing there?

  8. #8
    Chram
    Join Date
    Jun 2006
    Posts
    2,539
    BG Level
    7

    So basically you don't know anything at all.

    Programming isn't a subject where you can just sit down and 'learn to do this' and 'learn to do that'. You have to know your fundamentals and there is no way to avoid that. Grab yourself a c/c++ book and get to work.

    On a unrelated note I really hate programs that use the registry. It just isn't needed and makes things gay when I want to backup the program and use it on another computer, hi2u ffxi. I also hate these new rash of programmers who want to use database servers for everything!

    There are so many high performance hashing libraries to store key->data info to files there is just no excuse to use the registry for anything.

    For information that rarely changes I use http://cr.yp.to/cdb.html because its read performance is unbeatable and it doesn't even take a second for me to rewrite files several megs large.

    For information that needs to be updated often I just use berkeley db2, which has recently been taken over and updated by oracle for free. http://www.oracle.com/database/berkeley ... index.html

    I swear 99% of the websites/programs that use mysql could just as easily use db2 instead and run like 10x faster and not break as much. hi2u killingifrit.

  9. #9
    E. Body
    Join Date
    Jun 2006
    Posts
    2,181
    BG Level
    7
    FFXIV Character
    Bro Teampill
    FFXIV Server
    Gilgamesh
    FFXI Server
    Ifrit

    Devek is correct. If you don't understand what I posted you probably need to get a little more experience in coding. I did edit my post though with comments, maybe that will help to understand what's going on there.

    Devek - I agree. Registry just bloats stuff and makes it too difficult for average users to manually remove app based dependencies or old settings. That's why I suggested he used XML and local settings (which is what I like to do if I ever find myself in need of storing settings and other junk.)

  10. #10
    Relic Weapons
    Join Date
    Jan 2006
    Posts
    334
    BG Level
    4

    Yeah, i don't know much of anything except what we covered in a beginners course in high school, which is why I'm asking for help.

    The book I have from that class, Computer Science Tapestry, doesn't cover the information I'm looking for. So any help would be great.

    edit: thanks for the comments, i'll look over the code and work around with it. Thanks again.

  11. #11
    Chram
    Join Date
    Jun 2006
    Posts
    2,539
    BG Level
    7

    If you need books, there is one brand you can trust.. O'Reilly. Not all of their books are spectacular but none of them are a waste of money.

    http://www.oreilly.com/pub/topic/cprog

  12. #12
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    Quote Originally Posted by Devek
    If you need books, there is one brand you can trust.. O'Reilly. Not all of their books are spectacular but none of them are a waste of money.

    http://www.oreilly.com/pub/topic/cprog
    Nonsense, there are lots of non-o'reilly c++ books I trust:

    http://www.aristeia.com/books_frames.html

    http://www.josuttis.com/libbook/

    To name two. Windows Programming in C++ books that I trust? Well, there aren't any. Suggestions? And don't be bringing in any of that weak-sauce MFC shit in this house.

  13. #13
    Chram
    Join Date
    Jun 2006
    Posts
    2,539
    BG Level
    7

    Quote Originally Posted by Correction
    Quote Originally Posted by Devek
    If you need books, there is one brand you can trust.. O'Reilly. Not all of their books are spectacular but none of them are a waste of money.

    http://www.oreilly.com/pub/topic/cprog
    Nonsense, there are lots of non-o'reilly c++ books I trust:

    http://www.aristeia.com/books_frames.html

    http://www.josuttis.com/libbook/

    To name two. Windows Programming in C++ books that I trust? Well, there aren't any. Suggestions? And don't be bringing in any of that weak-sauce MFC shit in this house.
    Erm.. thre isn't a C++ book for windows.

    If you wanna learn win32 get Programming Windows by Charlez Petzold. It is the bible for that shit. With that you can write a windows program in any language if you really wanna.

  14. #14
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    Quote Originally Posted by Devek
    Quote Originally Posted by Correction
    Quote Originally Posted by Devek
    If you need books, there is one brand you can trust.. O'Reilly. Not all of their books are spectacular but none of them are a waste of money.

    http://www.oreilly.com/pub/topic/cprog
    Nonsense, there are lots of non-o'reilly c++ books I trust:

    http://www.aristeia.com/books_frames.html

    http://www.josuttis.com/libbook/

    To name two. Windows Programming in C++ books that I trust? Well, there aren't any. Suggestions? And don't be bringing in any of that weak-sauce MFC shit in this house.
    Erm.. thre isn't a C++ book for windows.

    If you wanna learn win32 get Programming Windows by Charlez Petzold. It is the bible for that shit. With that you can write a windows program in any language if you really wanna.
    MS Press put out a few C++ Windows Programming books but they were really really bad:

    http://www.amazon.com/Programming-Windo ... 1572316950

  15. #15
    Chram
    Join Date
    Jun 2006
    Posts
    2,539
    BG Level
    7

    Ya I know. The one I listed is really good and we have like 3 copies of it at work

  16. #16
    VZX
    VZX is offline
    Relic Shield
    Join Date
    Dec 2006
    Posts
    1,700
    BG Level
    6
    FFXI Server
    Asura

    If there's one thing I regret that I don't learn it in university, that'll be windows MFC

  17. #17
    Salvage Bans
    Join Date
    Aug 2005
    Posts
    866
    BG Level
    5
    FFXI Server
    Lakshmi

    if you want to program for windows only, and just want to have fun (i mean no big or complex apps) it's kinda dumb to use C++ IMO. Install Visual Studio Express for C# or VB.net and code your little stuff with these...you'll spend more time programming stuff, than wondering why it crashes all the time.

    http://msdn.microsoft.com/vstudio/express/

    If your goal is C++ and you don't wanna do anything else...buy a lot of coffee, painkillers, and start with MFC (Microsoft Foundation Classes). While disturbing for people with good C++ and/or low-level windows knowledge, MFC happen to be a good entry-point for people willing to learn. The book from MSPress - Windows Programming with MFC - is pretty good, despite all their other books being really shitty.

    edit: Back on your initial problem, the "Serialization" mechanisms are a good way to store data/app-state to a file for later retrieval. MFC offers binary and xml styles of serialization, a book will explain it all pretty well

    Quote Originally Posted by VZX
    If there's one thing I regret that I don't learn it in university, that'll be windows MFC
    I don't know if many companies still use MFC nowadays. And if you have lots of C/C++ and Windows programming experience, you're gonna want to kill yourself if you're asked to work with MFC only.

  18. #18
    E. Body
    Join Date
    Jun 2006
    Posts
    2,181
    BG Level
    7
    FFXIV Character
    Bro Teampill
    FFXIV Server
    Gilgamesh
    FFXI Server
    Ifrit

    Personally, I'd recommend people start with C/C++ and then move on to more abstracted frameworks or languages. It's a lot easier to move from a lower level to a higher level than the other way around. As mentioned, it does take a lot more time and patience, but provides a solid foundation to build on.

  19. #19
    Cerberus
    Join Date
    Apr 2006
    Posts
    498
    BG Level
    4
    FFXI Server
    Fenrir

    Honestly, self-teaching is fine.
    But I'd probably go for a refresher course from a community college. Hey, I admit... I used the course to get free and legit copies of Vis Studio, XP pro, and some other programs 8)

    Course and book: $400. Classtime and studying: a couple of hours per week. Coming away with over $2000 of software legitimately and free? Priceless

  20. #20
    Salvage Bans
    Join Date
    Aug 2005
    Posts
    866
    BG Level
    5
    FFXI Server
    Lakshmi

    Quote Originally Posted by Sigi
    Honestly, self-teaching is fine.
    But I'd probably go for a refresher course from a community college. Hey, I admit... I used the course to get free and legit copies of Vis Studio, XP pro, and some other programs 8)

    Course and book: $400. Classtime and studying: a couple of hours per week. Coming away with over $2000 of software legitimately and free? Priceless
    I wish stuff like that existed where I live :/
    As much as self-teaching is fine, sometimes you really need to have a teacher, so you don't fall asleep or double-click the ffxi icon "just to check on AH sales" ^^

Page 1 of 2 1 2 LastLast

Similar Threads

  1. A question to Yummy and Clistophy!!
    By Hirronimus in forum General Discussion
    Replies: 2
    Last Post: 2004-08-11, 00:14
  2. Just got a question for you guys
    By Blackwar in forum General Discussion
    Replies: 3
    Last Post: 2004-08-06, 20:14