Thread: Data from file (XML, Config, ..) to Object

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    Utrecht, The Netherlands
    Posts
    5

    Data from file (XML, Config, ..) to Object

    Hello!

    I've a class:
    Code:
    class Customer
    {
    public:
    	std::string get_name();
    	short get_gender();
    	int get_id();
    	Customer();
    private:
    	int id;
    	short gender;
    	std::string name;
    };
    And some data in a file (customer details).
    What is the best way to put this data into objects of Customer? Should I use XML, libconfig, or something else? I only want to read from a file, not to write data back.

    Thanks in advance for your help.
    Thomas Peter

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Maybe the better question would be - how does the file layout look like? Since you aren't writing to it, I don't know if you are defining the layout either.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Location
    Utrecht, The Netherlands
    Posts
    5
    I'm defining the layout too.
    So if XML is the best way to do this, I'll create a XML file for my data. And so for other layout types.
    And if it matters: I'm using gcc on a Fedora8 box, and use gtkmm in the program.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Easiest, least portable is binary. Harder, but most portable, XML.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Location
    Utrecht, The Netherlands
    Posts
    5
    I want to compile my program on both Linux and Windows, so XML is the best choice here because it's the most portable?
    Can someone give me an example to do this? (parse the elements of the XML into objects, and the subelements into the objects variables?)
    Thanks!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Binary is definitely possible. The only problem with binary is that if something is X size on one machine and X + 2 bytes on another machine, then it's not likely it's going to work too well reading that information again.
    I like binary, but I wouldn't recommend it. As for XML, it's best to get a parser off the web somewhere. Writing a parser can be complicated, even though XML is very simple to parse (at least if you compare to things like C/C++).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you go the binary route, one possibility to consider is SQLite, an embedded database engine. It has been described by its author as an alternative to fopen(), and you will pretty much have a format that can be consistent across Linux, Windows, and possibly Mac OS X, and other operating systems on which SQLite can run.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2008
    Location
    Utrecht, The Netherlands
    Posts
    5
    SQLite seems good to me, I'll give it a try. Is there a simple C++ wrapper (under GPL) for the C code?
    If so, I'd really like
    Thanks!

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is there a simple C++ wrapper (under GPL) for the C code?
    I uh, wrote a simple C++ wrapper for SQLite with the main goal of using RAII, but could not figure out how to handle the user defined function feature elegantly... so I have never published it.

    That said, there are other (probably better) C++ wrappers that you can try. Some are listed in the SQLite wiki's SQLite Wrappers page.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jan 2008
    Location
    Utrecht, The Netherlands
    Posts
    5
    Ah, great
    SQLite Wrapped seems good for my app, as I read the tutorial. Only problem that I can't get it to work on my Fedora 8 64bit box.
    But for now, thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Replies: 2
    Last Post: 03-03-2004, 07:25 PM