Store information

This is a discussion on Store information within the C++ Programming forums, part of the General Programming Boards category; Hi, i'm building a quiz. How should i store my information about the highscore? Maybe in a textfile on the ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    188

    Store information

    Hi, i'm building a quiz. How should i store my information about the highscore?
    Maybe in a textfile on the net, but i'm not good at C++ yet, so i don't know.

    I want that everybody can have their own copy of this program, so i figured, i must have the highscore on the internet otherwise everybody aint gonna have the same highscore, right?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Right.

    First thing to do is to save it to a text file or something, then you could use putty (That it?), to upload it to your site or something. I'm sure it has command line arguments which you could manipulate.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You have a PHP backend that logs highscore data sent by the players in some format, xml, your own format, whatever, and then also serves queries to get the high score list. You can use a library like libcurl http://curl.haxx.se/ to ease this part of your program on the client side (it's very easy to use)

    >> I want that everybody can have their own copy of this program, so i figured, i must have the highscore on the internet otherwise everybody aint gonna have the same highscore, right?

    My gameboy only has my highscores for Tetris. I think it's better for my esteem that it is that way.

    >> putty

    Mm, I can only think of very impractical ways to use PuTTY for this app.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Hmm, i think i prefer PuTTY even though i'm great at PHP. Is PuTTY hard to code?

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Did you even Google it?

    Go for curl. It's a much better idea! You can use curl with C programs too, as well as with PHP. You can upload files to a site very easily if you make a very simply PHP script to accept the files. Download it and install it, and use Tonto's code.
    Last edited by twomers; 04-07-2007 at 02:52 PM.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    But how do i get recent data from the file before i overwrite the recent file?

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Use curl to download the high scores too.

    Or you could use a database (mysql or something), which the php script, when a score is uploaded to it, adds elements to, so you only upload your current score. Store them in the DB, and print the high score on a page on the net in ascending or descending order according to your preference. Then download the contents of the high score using curl if you want do display them in your program. It's more of a PHP problem then, the high-score table that is. The C problem would be uploading/downloading the webpage highscore, which shouldn't really be problematic once you get to grips with curl.

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    This seems a bit tricky. I think i'm changing how to store the ifno. I want to have a different highscore for different program. So i can skip cURL. How is this possible?

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Good idea. Once you have that fixed you might consider looking into curl.

    Just store the info to file, and then when you read in the score sort them in memory. It's much easier to do that than it is to reformat the file.

    Say for example this was your highscore table:

    twomers 1447
    Livijn 54
    Tonto 1306

    You could read the files into a map<string, int> and sort with respect to the int part, then display it if you want.

    If the question was "How do I save and read from and to files", look at the faq or the tutorials on the mother site.

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Okey, i think i need more help.
    I've read the tutorial on the mother site, and i now how to open files. But after that,
    how do i read from them? It doesn't sais how.

    Code:
    Code:
    void file_read()
    {
           string fs;
           
           ifstream the_file ("highscore.txt");
           if ( !the_file.is_open() ) {
                cout << "Error opening file!";
           } else {
                  // Read anc cout text!
           }
    }

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    There are a number of options for reading in a file. We'll say you want to read them to a map of strings and ints, for the name and score.

    This is probably going to look like a handful, but it's not too hard to understand!


    Code:
    #include <iostream>
    #include <fstream>
    #include <map>
    
    int main( void )
    {
        std::ifstream in( "scores.txt" );
    
        if ( !in )
            return 1; // or something
    
        std::map< std::string, int > scoresheet;
        std::string str;
        int score;
    
        while ( in >> str >> score )
            scoresheet[str] = score;
    
        std::map<std::string,int>::iterator iter;
        for ( iter=scoresheet.begin(); iter!=scoresheet.end(); iter++ )
        {
            std::cout<< iter->first << " got " << iter->second << " points!" << std::endl;
        }
    
        return 0;
    }
    it works for me, with the test highscore that I gave earlier. Try it out and see if you understand it. If not come back.

  12. #12
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Can someone explain the code. I don't just wanna paste and copy if you understand.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Basically, you read from an ifstream just like cin. Maps are like arrays but with other data types for their indicies. Strings are like character arrays but more convienient.

    Have a look at these, or the relavent sections in a textbook.
    Learning to Program in C++: File I/O: http://www.cprogramming.com/tutorial/lesson10.html
    cppreference.com: Maps: http://cppreference.com/cppmap/index.html
    cppreference.com: Strings: http://cppreference.com/cppstring/index.html
    cppreference.com: C++ I/O: http://cppreference.com/cppio/index.html

    [offtopic] What, no codeform? Just kidding. [/offtopic]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    I didn't like your example... It was so messy
    I got this:
    Code:
    int file_read( void )
    {   
        string high;
        ifstream in( "highscore.txt" );
        
        while (in >> high) {
        cout << high <<;
        }
        
        in.close();
        cin.get();
        return 0;
    }
    Now everything comes this way, Fredrik,2Derik,3, etc... How can i break row after the point?

    EDIT:

    I now did like this and it works. But i want a better solution...
    Code:
    int file_read( void )
    {   
        int dd = 1;
        string high;
        ifstream in( "highscore.txt" );
        
        while (in >> high) {
        cout << high;
        if (dd == 2 || dd == 4 || dd == 6 || dd == 8 || dd == 10 || dd == 12) {
        cout << "\n";
        }
        dd++;
        }
        
        in.close();
        cin.get();
        return 0;
    }
    Last edited by Livijn; 04-16-2007 at 10:30 AM.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    if (dd == 2 || dd == 4 || dd == 6 || dd == 8 || dd == 10 || dd == 12) {
    Hint: modulus. http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to get information from cpuid using c code
    By ZeroMemory in forum C Programming
    Replies: 2
    Last Post: 07-01-2008, 03:03 AM
  2. CPU information
    By Newmer60 in forum C Programming
    Replies: 9
    Last Post: 06-17-2008, 04:00 PM
  3. Using a table to store PCB information?
    By John_L in forum Tech Board
    Replies: 2
    Last Post: 06-09-2008, 03:05 AM
  4. Extract values and store in new variable
    By cosmiccomputing in forum C Programming
    Replies: 3
    Last Post: 06-01-2008, 02:45 PM
  5. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 07:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21