CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2009
    Posts
    4

    Using binary files as database?

    Ok, let's rephrase that cause after reading what I typed it didn't really made sense...

    I want to save character info (yes, it's for a game)

    With ini files the file has a structure like this:
    Code:
    [Username]
    Password= something
    Security=Stuff
    email=mail
    Character profession= prof
    level=char level
    hit points= hp
    skills= skills char has
    items= char's items
    etc.
    Which makes it easy to load/save and serves as a easy editable database.

    But I want to use binary files instead of ini files (cause I read that binary files were faster with loading)
    But I just can't seem to correctly save or read the files...




    Original post:

    So I'm trying to code something that saves/reads accounts in binary files but since this is the first time ever that I tried to do something with binary files, I'm kinda stuck.
    _____

    So let's say that I want to save/read:

    Example.dat (where Example is the username)
    Password
    e-mail
    security Q/A

    How would I do that?
    Last edited by Creqaw; August 20th, 2009 at 10:08 AM.

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Using binary files as database?

    Could you please clarify....

    Where will you be storing the information. In an SQL database or a File System?
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Sep 2006
    Posts
    199

    Re: Using binary files as database?

    There is an embedded sql-like database that I've seen but cannot remember what it's called.

  4. #4
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Using binary files as database?

    After writing/reading the data to the files try to use some encryption technique so that no can hack the data even if they open it , it should be in binary format - non - readable.

  5. #5
    Join Date
    Aug 2009
    Posts
    4

    Re: Using binary files as database?

    Quote Originally Posted by rliq View Post
    Could you please clarify....

    Where will you be storing the information. In an SQL database or a File System?
    I'm doing this so that I don't need a SQL database.

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: Using binary files as database?

    So what you're saying is that you don't need or want a database? If so, why are you asking how to use a binary file as a database?

    Maybe you should describe what you want rather than how you think it should be done. Do you want to know how you can read/write data from a file?
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  7. #7
    Join Date
    Aug 2009
    Posts
    4

    Re: Using binary files as database?

    Ok, let's rephrase that cause after reading what I typed it didn't really made sense...

    I want to save character info (yes, it's for a game)

    With ini files the file has a structure like
    Code:
    [Username]
    Password= something
    Security=Stuff
    email=mail
    Character profession= prof
    level=char level
    hit points= hp
    skills= skills char has
    items= char's items
    etc.
    Which makes it easy to load/save.

    But I want to use binary files instead of ini files (cause I read that binary files were faster with loading)
    But I just can't seem to correctly save or read the files...

  8. #8
    Join Date
    May 2007
    Posts
    1,546

    Re: Using binary files as database?

    But I want to use binary files instead of ini files (cause I read that binary files were faster with loading)
    For your case, I think you should just stick to just writing things to a text file in the exact format you described above. Performance is not going to be an issue. I'd be very surprised if you could even find the load/save routines in the output of a profiler.

    The only reason why the above format would not be ideal would be because the user can easily hand-edit it and change their stats. One way to prevent that would be to append the SHA1 sum (or something similar) of the data to the end of the file. Then, when you read the data into memory, you can verify it has not been altered.

    There are other ways, but that's probably one of the easiest ones to do and it's moderately difficult to bypass.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  9. #9
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Using binary files as database?

    Put the data in a DataSet inside your app. Then you can use Read/WriteXML() to save the data to a file system. You could also pipe that XML through an encryption routine when saving and decryption routine when loading and also change the file extension to be .DAT instead of .XML.

    For example...

    Code:
            public static void SaveDataSet(String fileName, String password, DataSet ds)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    ds.WriteXml(ms);
    
                    // your encryption object - you need to do this bit
                    RijndaelEnhanced rje = new RijndaelEnhanced(password);
    
                    File.WriteAllBytes(fileName, rje.EncryptToBytes(ms.ToArray()));
    
                    ms.Close();
                }
            }
    
            public static void LoadDataSet(String fileName, String password, DataSet ds)
            {
                // your decryption object - you need to do this bit
                RijndaelEnhanced rje = new RijndaelEnhanced(password);
    
                using (MemoryStream ms = new MemoryStream(rje.DecryptToBytes(File.ReadAllBytes(fileName))))
                {
                    ds.ReadXml(ms);
    
                    ms.Close();
                }
            }
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  10. #10
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Using binary files as database?

    Quote Originally Posted by Mutant_Fruit View Post
    I'd be very surprised if you could even find the load/save routines in the output of a profiler.
    I could not understand this. Can you please throw some more light on this - what is profiler here ?

    Quote Originally Posted by Mutant_Fruit View Post
    One way to prevent that would be to append the SHA1 sum (or something similar) of the data to the end of the file.
    Can you explain more abt SHA1 ? whats the full form of it ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured